【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. JS之字符串和数组

    字符串: 属性: length:可以获取字符串的长度: <script type="text/javascript"> var str = "haha&quo ...

  2. 紫书 例题 9-6 UVa 11400 (线性结构上的动态规划)

    这道题的下标从1开始比较方便,一方面前缀和算的方便一些,一方面涉及到前j 个灯泡,那么如果从0开始,前3个灯泡就是第0, 1, 2, 3个,非常奇怪. 所以灵活换下标. 然后这道题的动规有点暴力枚举的 ...

  3. qt程序实现打开文件夹

    QString path=QDir::currentPath();//获取程序当前目录 path.replace("/","\\");//将地址中的" ...

  4. sqoop 1.4.4-cdh5.1.2高速入门

    一.高速入门 (一)下载安装 1.下载并解压 wget http://archive.cloudera.com/cdh5/cdh/5/sqoop-1.4.4-cdh5.1.2.tar.gz tar - ...

  5. iOS定制改动navigation的backbutton

    iOS开发中.navigation的返回button是英文"back".想改动成中文"返回"或者自己定义的文字.这么简单的事情却折腾了小半个小时.原来是被lef ...

  6. 把文件保存到 sdcard

    直接上代码: package com.example.test; import java.io.File; import java.io.FileNotFoundException; import j ...

  7. 操作系统的 (program)loader(程序加载器)

    在计算机科学中,加载器(也叫程序加载器)属于操作系统的一部分,用于加载程序(programs)和库(libraries).加载器是执行程序和代码必不可少的组件,正是它负责将程序送入内存,为程序的运行提 ...

  8. 如何监控和解决SQL Server的阻塞(1) (当前阻塞)

    1. 什么是"阻塞"? 阻塞是SQL数据库应用"锁"机制的一个副作用.当一个应用请求针对某个数据库对象(例如全表,某行数据, 或者是某个数据页)加锁后,那么这个 ...

  9. java(数组及常用简单算法 )

    数组 数组:数组是存储同一种数据类型数据的集合容器. 数组的定义格式: 数据类型[]  变量名  =  new  数据类型[长度]; 数组的好处:对分配到数组对象中每一个数据都分配一个编号(索引值.角 ...

  10. BZOJ3511: 土地划分(最小割)

    Description Y国有N座城市,并且有M条双向公路将这些城市连接起来,并且任意两个城市至少有一条路径可以互达. Y国的国王去世之后,他的两个儿子A和B都想成为新的国王,但他们都想让这个国家更加 ...