8.8 LeetCode 199 Binary Tree Right Side View

Question

link

 

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

For example:
Given the following binary tree,

   1            <---
 /   \
2     3         <---
 \     \
  5     4       <---

 

You should return [1, 3, 4].

 

Show Tags
Tree Depth-first Search Breadth-first Search

Solution

My own solution is BFS and take the last element of each level into a list, but we need two lists so we can make sure clear one level before clear next level. The solution from NineChart uses the feature that the num of seen nodes equals the level, so check right first, if not null put in list, then check left if num < level indicates right is null, put left into list or right is not null, do nothing.

My solution I (No code reuse):

public List<Integer> rightSideView(TreeNode root) {
        LinkedList<Integer> res = new LinkedList<Integer>();
        if(root == null) 
            return res;
        LinkedList<TreeNode> list1 = new LinkedList<TreeNode>();
        LinkedList<TreeNode> list2 = new LinkedList<TreeNode>();
        list1.add(root);
        int cnt = 1;
        while(!list1.isEmpty() || !list2.isEmpty()) {
            while(!list1.isEmpty()) {
                TreeNode temp = list1.remove();
                if(list1.isEmpty())
                    res.add(temp.val); // list is null indicate this is the rightest node
                if(temp.left != null) {
                    list2.add(temp.left);
                }
                if(temp.right != null) {
                    list2.add(temp.right);
                } 
            }
            while(!list2.isEmpty()) {
                TreeNode temp = list2.remove();
                if(list2.isEmpty())
                    res.add(temp.val); // list is null indicate this is the rightest node
                if(temp.left != null) {
                    list1.add(temp.left);
                }
                if(temp.right != null) {
                    list1.add(temp.right);
                }
            }
        }
        return res;
    }

My solution II (improved by reuse the code of iteration):

public List<Integer> rightSideView(TreeNode root) {
        LinkedList<Integer> res = new LinkedList<Integer>();
        if(root == null) 
            return res;
        LinkedList<TreeNode> list1 = new LinkedList<TreeNode>();
        LinkedList<TreeNode> list2 = new LinkedList<TreeNode>();
        list1.add(root);while(!list1.isEmpty() || !list2.isEmpty()) {
            bfs(list1, list2, res); // remove nodes from list1 and put its children into list2
            bfs(list2, list1, res); // remove nodes from list2 and put its children into list1
        }
        return res;
    }
    private void bfs(LinkedList<TreeNode> list1, LinkedList<TreeNode> list2, LinkedList<Integer> res) {
        while(!list1.isEmpty()) {
            TreeNode temp = list1.remove();
            if(list1.isEmpty())
                res.add(temp.val); // list is null indicate this is the rightest node
            if(temp.left != null)
                list2.add(temp.left);
            if(temp.right != null)
                list2.add(temp.right);
        }
    }

Solution of NineCharts (More concise use more time but less space):

public class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        traverse(result, root, 1);
        return result;
    }

    private void traverse(List<Integer> result, TreeNode node, int level) {
        if (node == null) return;
        if (level > result.size()) {
            result.add(node.val);
        }
        traverse(result, node.right, level + 1);
        traverse(result, node.left, level + 1);
    }
}

 

posted @ 2015-08-09 00:23  YoungAndSimple  阅读(128)  评论(0编辑  收藏  举报