My Java solution using BFS. any suggestion?


  • 0
    X
    public List<Integer> rightSideView(TreeNode root) {
    	List<Integer> list = new ArrayList<Integer>();
    	if (root == null)
    		return list;
    	else {
    		Queue<TreeNode> q = new LinkedList<TreeNode>();
    		q.add(root);
    		while (!q.isEmpty()) {
    			int rowSize = q.size();
    			for (int i = 0; i < rowSize; i++) {
    				TreeNode c = q.poll();
    				if (i == 0)
    					list.add(c.val);
    				if (c.right != null)
    					q.add(c.right);
    				if (c.left != null)
    					q.add(c.left);
    			}
    		}
    		return list;
    	}
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.