// 我的代码
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. Doctype的作用

    <!DOCTYPE> 声明不是 HTML 标签:它是指示 web 浏览器关于页面使用哪个 HTML 版本进行编写的指令,也就是定义文档类型. 所有的浏览器都需要两种模式:怪异模式和严格模式 ...

  2. Mysql上手

    使用Mysql,打开 相应的服务.启动-- 打开命令窗口.此处有多种方法,我是在开始菜单(Mysql5.6 Command Line Client)打开的(简单). mysql -h localhos ...

  3. Windows无线网“无法连接到这个网络”的解决办法

    最近我的笔记本(Win10系统)连接无线网时总是出现"无法连接到这个网络"的错误.为此折腾了一天,通过各种百度,终于找到了适合我的解决办法,在此记录一下. 问题描述 最近因为宿舍的 ...

  4. python之路十三

    前景介绍 到目前为止,很多公司对堡垒机依然不太感冒,其实是没有充分认识到堡垒机在IT管理中的重要作用的,很多人觉得,堡垒机就是跳板机,其实这个认识是不全面的,跳板功能只是堡垒机所具备的功能属性中的其中 ...

  5. VS2008控件全部消失

    新建VS2008项目之后,本该位于工具箱的控件全部消失不见,只剩下"#13119"提示,修复方法如下: 注:不一定三步都需要用到,仅在当前步骤无效情况下才用到下一步 1.步骤一 ( ...

  6. Ubuntu16.04安装Atom

    转自:http://blog.csdn.net/q1302182594/article/details/51304401 sudo add-apt-repository ppa:webupd8team ...

  7. Eclipse下载与安装

    有很多小伙伴不知道eclipse在哪里下载以及怎么安装的,那我来给大家做个演示吧. 学习java比较常用的软件就是eclipse,而eclipse安装后并不能马上使用,还需要安装jdk并搭建环境.下面 ...

  8. 在js中添加新节点

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  9. Weblogic集群

    http://dead-knight.iteye.com/blog/1942514 http://www.cnblogs.com/HondaHsu/p/4267972.html#undefined

  10. Tips标签显示

    浏览地址 http://www.7yusou.com 鱼搜官网 下载到本地