Simple Iterative Java solution : BFS


  • 0
    S

    Re: Reverse Level Order Traversal

     public List<Integer> rightSideView(TreeNode root) {
    		List<Integer> sol = new LinkedList<Integer>();
    		TreeNode node = null;
    		Queue<TreeNode> que = new LinkedList<TreeNode>();
    		if(root == null) return sol;
    		que.offer(root);
    		while(!que.isEmpty()){
    			int length = que.size();
    			for(int i =0 ; i < length ; i++){
    				node = que.poll();
    				if(node.left != null) que.offer(node.left);
    				if(node.right != null)que.offer(node.right);
    			}
    			sol.add(node.val);
    		}
    		return sol;
    	    }
    
    

Log in to reply
 

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