// 我的代码
package Leetcode;
/**
* 199. Binary Tree Right Side View
* address: https://leetcode.com/problems/binary-tree-right-side-view/
* 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.
*
*/
import java.util.*; public class BinaryTreeRightSideView {
public static void main(String[] args) {
TreeNode[] tree = new TreeNode[9];
for (int i = 1; i < 9; i++){
tree[i] = new TreeNode(i);
}
TreeNode.link(tree, 1, 2, 3);
TreeNode.link(tree, 2, 4, 5);
TreeNode.link(tree, 3, 6, -1);
TreeNode.link(tree, 5, 7, 8);
// 先序遍历
TreeNode.prePrint(tree[1]);
// solution
List<Integer> result = rightSideView(tree[1]);
System.out.println(result); // solution2
List<Integer> result2 = rightSideView2(tree[1]);
System.out.println(result2); }
/**
* 方法一:时间复杂度较高,需要 O(n),n为树中节点的个数
* @param root
* @return
*/
public static List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
int childNum = 0;
if (root != null){
queue.offer(root);
while(!queue.isEmpty()){
TreeNode node = queue.poll();
if(node.left!= null){
queue.offer(node.left);
childNum++;
}
if (node.right != null)
{
queue.offer(node.right);
childNum++;
}
System.out.println("queue.size() = " + queue.size());
if (childNum - queue.size() == 0){
result.add(node.val);
childNum = 0;
}
}
} return result; }
 /**
* 方法二
* 别人家的解法,巧妙,速度快且好理解
* @param root
* 中右左 深度优先遍历,保存每层的第一个节点。用当前结果表的size作为标识,这样保证每次存的节点都是第一次出现在该层的节点
* @return
*/
public static List<Integer> rightSideView2(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
if (root == null){
return res;
}
dfs (root, res, 0);
return res;
} public static void dfs (TreeNode root, List<Integer> res, int level){
if (root == null){
return;
}
if (res.size() == level){
res.add (root.val);
}
if (root.right != null){
dfs (root.right, res, level + 1);
}
if (root.left != null){
dfs (root.left, res, level + 1);
}
}

leetcode 199 :Binary Tree Right Side View的更多相关文章

  1. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  2. [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 ...

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

  4. [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 ...

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

  6. (二叉树 bfs) 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 ...

  7. [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 解题报告(Python)

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

  9. 【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, ...

随机推荐

  1. centos 6.7 搭建tornado + nginx + supervisor的方法(已经实践)

    首先,本来不想写这篇博客了,但是我测试了很多网上的例子包括简书的,全不行,我总结原因是自己太笨,搞了俩个晚上,后来决定,自己还是写一篇记录下来,保证自己以后使用 环境: centos6.7 64 py ...

  2. module 和 module.exports 的区别

    自己理解的不好,推荐一篇文章吧:   http://www.cnblogs.com/pigtail/archive/2013/01/14/2859555.html

  3. DevExpress 程序运行后 layoutView 卡片大小发生变化

    设置属性前效果:  将layoutView1.CardMinSize 的属性更改为(0, 0) 后 效果:

  4. PHP Ueditor 富文本编辑器

    2016年12月11日 08:46:59 星期日 百度的简版富文本编辑器umeditor很久没更新了 全功能版本的配置项跟umeditor还是有区别的, 这里说下ueditor怎么对接到项目中去, 主 ...

  5. shell 输出九九乘法表

    #/bin/bash i= j= )) do while(($j<=$i)) do echo -ne $j×$i=$[$j*$i]"\t" j=$[$j+] done ech ...

  6. Web 前端之HTML和CSS

    Web 前端之HTML和CSS HTML被称为超文本标记语言(Hyper Text Markup Language),它不是一种编程语言,而是一种标记语言,标记语言是一套标记标签,HTML使用标记标签 ...

  7. Xcode 常用快捷键

    一.Xcode基本快捷键 1.1.新建项目 Shift + CMD + N 1.2.项目中新建文件 CMD + N 1.3.运行 CMD + R 1.4.编译 CMD + B 1.5.停止运行 CMD ...

  8. 关于ZedGraph

    http://www.codeproject.com/Articles/5431/A-flexible-charting-library-for-NET

  9. Java Graphics2D 画出文字描边效果

    在CSDN看到的,在此记下. (http://bbs.csdn.net/topics/390703095) import javax.swing.*; import java.awt.*; impor ...

  10. scala eclipse plugin 插件安装

    最近在看Apache Apollo 代码,其中有很多scala代码,没办法需要安装一个scala插件. 我试过zip 安装,直接下载的update-site.zip 不能直接安装到位.我又特别懒,不想 ...