// 我的代码
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. Jni :三维数组处理方法 ,以整形三维数组为例 C++实现

    本文原创,转载请注明地址:http://www.cnblogs.com/baokang/p/4982846.html 关于Jni的基本使用方法,请参阅:Java 调用 C++ (Java 调用 dll ...

  2. Best Time to Buy and Sell Stock1,2,3,4

    找到最低值和最高值 int maxProfit(vector<int>& prices) { ); ; ]; ;i<prices.size();i++) { profit=m ...

  3. git知识点整理

  4. 项目管理的一些Tip

    最近带领了一个大概150人天的项目,做一些备注: 1, 知人善用. 排期分任务的时候,要先了解每个团队成员的特点,包括技术水平.性格,看他是适合做技术难度大的模块还是适合做业务逻辑复杂的模块. 2, ...

  5. 【spoj705】 Distinct Substrings

    [题目描述] 给定一个字符串,计算其不同的子串个数. [输入格式] 一行一个仅包含大写字母的字符串,长度<=50000 [输出格式] 一行一个正整数,即不同的子串个数. [样例输入] ABABA ...

  6. oracle学习笔记(二)

    1. Oracle字符串操作 1.1. 字符串类型 1.1.1. CHAR和VARCHAR2类型 CHAR和VARCHAR2类型都是用来表示字符串数据类型,用来在表中存放字符串信息, 比如姓名.职业. ...

  7. 常用ADO.NET操作ACCESS数据库

    using System; using System.Collections.Generic; using System.Text; using System.Data;// using System ...

  8. Python全栈开发【面向对象进阶】

    Python全栈开发[面向对象进阶] 本节内容: isinstance(obj,cls)和issubclass(sub,super) 反射 __setattr__,__delattr__,__geta ...

  9. 高可用thrift客户池的实现详解

    最近,公司要求将组内的thrift客户端组件推广至公司内使用.基本的要求如下: 1.高可用 2.集成名称服务,也就配置文件支持服务发现 3.解耦,客户端和高可用组件解耦,简单来说就是,如果以后要切换其 ...

  10. iOS 内存管理

    一 . 内存管理 包括内存分配 和 内存清除 1.内存管理的范围 :人和继承于NSObject类的对象都需要进行内存管理,任何非对象类型的对象(基本数据类型 如 int char float doub ...