【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. BZOJ3130: [Sdoi2013]费用流(二分,最大流)

    Description Alice和Bob在图论课程上学习了最大流和最小费用最大流的相关知识.    最大流问题:给定一张有向图表示运输网络,一个源点S和一个汇点T,每条边都有最大流量.一个合法的网络 ...

  2. 【TC SRM 718 DIV 2 B】Reconstruct Graph

    [Link]: [Description] 给你两个括号序列; 让你把这两个括号序列合并起来 (得按顺序合并) 使得组成的新的序列为合法序列; 即每个括号都能匹配; 问有多少种合并的方法; [Solu ...

  3. 【Uva 1625】Color Length

    [Link]: [Description] 给你两个序列,都由大写字母组成; 每次,把两个序列中的一个的开头字母加在字符串的尾端,然后在那个序列中删掉那个开头字母; 最后得到一个字符串; 这个字符串显 ...

  4. 我的RTOS 之二 --Threadx在skyeye上仿真測试(基于2410)

    对于RTOS 移植来说,移植平台至少要提供双方面的设备. 1.OS执行时,须要tick,所以须要提供Timer定时器 2.OS执行时,须要调度,就是挂起当前线程,把控制权交给系统,所以须要訪问系统各个 ...

  5. RvmTranslator6.3 is released

    RvmTranslator6.3 is released eryar@163.com RvmTranslator can translate the RVM file exported by AVEV ...

  6. LinkCutTree详解

    LCT详解 没有比这再详细的了, 相信我

  7. Linux下VsFTP和ProFTP用户管理高级技巧 之一

    Linux下VsFTP和ProFTP用户管理高级技巧       FTP服务时互联网上比较古老的一种应用,至今Interner应用面非常广泛,但令管理员头痛不已的是其用户管理,既多且杂,如何解决这一问 ...

  8. 【Django】缓存

    由于Django是动态网站,所以每次请求都会去数据库中进行响应的操作. 当程序访问量大时,耗时必然会更加明显,最简单的解决方案就是使用缓存. Django中的缓存: ==即将某一个view的返回值保存 ...

  9. Flask的快速入门详细笔记

    Flask的框架结构对应关系及理解 1.简介 简单介绍下Flask是一个轻量级的web前端框架,不像django那样本身具备一套完整的页面体系,轻量级说明了完全可以自定义,从功能逻辑到业务处理,都可以 ...

  10. [Angular] Make a chatbot with DialogFlow

    Register a account on https://console.dialogflow.com/api-client/ "Creat a intent" -- you c ...