【199-Binary Tree Right Side View(从右边看二叉树】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】


代码下载【https://github.com/Wang-Jun-Chao】

原题

  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].

题目大意

  给定一个二叉树,想象自己站在树的右边,返回从下到下你能看到的节点的值。

解题思路

  二叉树的层次遍历。每层依照从左向右的顺序依次訪问节点,(每一层取最右边的结点)

代码实现

树结点类

public class TreeNode {
int val;
TreeNode left;
TreeNode right; TreeNode(int x) {
val = x;
}
}

算法实现类

public class Solution {

    public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new LinkedList<>(); if (root != null) {
Deque<TreeNode> deque = new LinkedList<>();
// 当前层的结点数
int current = 1;
// 下一层的结点数
int next = 0;
TreeNode node;
deque.addLast(root);
while (deque.size() > 0) {
// 取第一个结点
node = deque.removeFirst();
current--; // 加入非空的左结点
if (node.left != null) {
next++;
deque.addLast(node.left);
} // 加入非空的右结点
if (node.right != null) {
next++;
deque.addLast(node.right);
} // 假设当前层已经处理完了
if (current == 0) {
// 保存此层的最右一个结点值
result.add(node.val);
// 设置下一层的元素个数
current = next;
next = 0;
}
}
} return result;
}
}

评測结果

  点击图片,鼠标不释放,拖动一段位置。释放后在新的窗体中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47970785

【LeetCode-面试算法经典-Java实现】【199-Binary Tree Right Side View(从右边看二叉树)】的更多相关文章

  1. 199. Binary Tree Right Side View 从右侧看的节点数

    [抄题]: Given a binary tree, imagine yourself standing on the right side of it, return the values of t ...

  2. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  3. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  4. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  5. 【LeetCode】199. Binary Tree Right Side View

    Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...

  6. 【刷题-LeetCode】199 Binary Tree Right Side View

    Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...

  7. Java for LeetCode 199 Binary Tree Right Side View

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  8. [LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  9. leetcode@ [199] Binary Tree Right Side View (DFS/BFS)

    https://leetcode.com/problems/binary-tree-right-side-view/ Given a binary tree, imagine yourself sta ...

  10. LeetCode OJ 199. Binary Tree Right Side View

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

随机推荐

  1. vim7.4官方源码在vs2013的编译方法及问题总结

    vim7.4发布也有一段时候了,也该是把之前编译的7.3重新编译一下了,于是考虑着到最新的visual studio 2013编译一下,也顺便看看有没有其它问题. 1.安装vs2013,这个应该不用说 ...

  2. Android学习总结(1)——好的 Android 开发习惯

    Android编码规范 java代码中不出现中文,最多注释中可以出现中文: 局部变量命名.静态成员变量命名:只能包含字母,单词首字母出第一个都为大写,其他字母都为小写: 常量命名:只能包含字母和 ,字 ...

  3. css中margin上下外边距重叠问题

    css的盒子模型里是这样规定两个对象之间的距离的:对象之间的间距是由两个对象的盒子模型的最终计算值得出来的,也就是说两个对象之间的间距就是两个对象的距离,但是当遇到两个对象一个有下外边距margin, ...

  4. 洛谷P3383 【模板】线性筛素数(Miller_Rabin)

    题目描述 如题,给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内) 输入输出格式 输入格式: 第一行包含两个正整数N.M,分别表示查询的范围和查询的个数. 接下来M行每行 ...

  5. 如何优雅的写UI——(4)选项卡美化

    现在做出来的选项卡实在太丑的,咱们怎么把他弄得好看一点呢 tabctrl是可以添加icon图标的,那派生与tabctrl的mfctabctrl肯定也能添加图标,他们两个添加图标的原理一样,但是还是有点 ...

  6. Linux 获取上个月的第一秒和上个月的最后一秒

    因为写脚本需求须要获得上个月的第一秒和上个月的最后一秒,查阅了相关资料,并通过自己实践.找到了以下这样的方法能满足要求.在此备注,若有其它好的方法.请留言.本人将不胜感激. 获取上个月的第一秒: da ...

  7. jquary依据td中button的元素属性删除tr行(删选出想删除的行)

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcnVveXVhbnlp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  8. 【Android】利用安卓的数据接口、多媒体处理编写内存卡Mp3播放器app

    通过调用安卓的MediaPlayer能够直接完毕Mp3等主流音频的播放,同一时候利用ContentResolver与Cursor能够直接读取安卓内在数据库的信息.直接获取当前sdcard中全部音频的列 ...

  9. JS ajax 应用 (下拉列表联动)

        <script language="javascript"> var http_request=false;    function send_request( ...

  10. android图像处理(3)底片效果

    这篇将讲到图片特效处理的底片效果.跟前面一样是对像素点进行处理,算法是通用的. 算法原理:将当前像素点的RGB值分别与255之差后的值作为当前点的RGB值. 例: ABC 求B点的底片效果: B.r ...