zoukankan      html  css  js  c++  java
  • 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;
        }
    }
    
  • 相关阅读:
    SSH服务附带----SFTP
    SSH附带的远程拷贝----SCP
    linux下的SSH服务
    model.form使用,配合form的钩子
    import_module 导入变量的包
    dir函数
    python爬虫之scrapy
    python爬虫之解析库Beautiful Soup
    django 过滤器,标签
    django 验证码实现
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8745616.html
Copyright © 2011-2022 走看看