首页 > 其他 > 详细

8.8 LeetCode 199 Binary Tree Right Side View

时间:2015-08-09 01:49:35      阅读:291      评论:0      收藏:0      [点我收藏+]

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);
    }
}

 

8.8 LeetCode 199 Binary Tree Right Side View

原文:http://www.cnblogs.com/michael-du/p/4714301.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!