199 Binary Tree Right Side View 二叉树的右视图

给定一棵二叉树,想象自己站在它的右侧,返回从顶部到底部看到的节点值。
例如:
给定以下二叉树,
   1            <---
 /  
2     3         <---
     
  5     4       <---
你应该返回 [1, 3, 4]。

详见:https://leetcode.com/problems/binary-tree-right-side-view/description/

Java实现:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> res=new ArrayList<Integer>();
        if(root==null){
            return res;
        }
        LinkedList<TreeNode> que=new LinkedList<TreeNode>();
        que.offer(root);
        int node=1;
        while(!que.isEmpty()){
            root=que.poll();
            --node;
            if(root.left!=null){
                que.offer(root.left);
            }
            if(root.right!=null){
                que.offer(root.right);
            }
            if(node==0){
                res.add(root.val);
                node=que.size();
            }
        }
        return res;
    }
}
原文地址:https://www.cnblogs.com/xidian2014/p/8745616.html