博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Binary Tree Right Side View
阅读量:4545 次
发布时间:2019-06-08

本文共 1538 字,大约阅读时间需要 5 分钟。

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

Top view, Bot view, right view, and left view.

1 /** 2  * Definition for binary tree 3  * public class TreeNode { 4  *     int val; 5  *     TreeNode left; 6  *     TreeNode right; 7  *     TreeNode(int x) { val = x; } 8  * } 9  */10 public class Solution {11     public List
rightSideView(TreeNode root) {12 List
result = new ArrayList
();13 if(root == null) return result;14 LinkedList
queue = new LinkedList
();15 queue.add(root);16 queue.add(null);17 int level = 0;18 while(queue.size() != 1){19 TreeNode tmp = queue.poll();20 if(tmp == null){21 queue.add(null);22 level ++;23 }else{24 if(result.size() == level){25 result.add(tmp.val);26 }else{27 result.set(level,tmp.val);28 }29 if(tmp.left != null)queue.add(tmp.left);30 if(tmp.right != null)queue.add(tmp.right);31 }32 }33 return result;34 } 35 }

 

转载于:https://www.cnblogs.com/reynold-lei/p/4395346.html

你可能感兴趣的文章
hdu 1264
查看>>
hdu 1273不会的题
查看>>
(转)父子窗体的菜单合并及工具栏合并
查看>>
分页SQL
查看>>
linux系统使用sh文件传参数给matlab程序
查看>>
软工实践原型设计-黄紫仪
查看>>
食用指南
查看>>
CSS3圆角详解(border-radius)
查看>>
Python正则表达式指南
查看>>
前端学习之JavaScript中的 NaN 与 isNaN
查看>>
chrome安装json view插件
查看>>
CSS div 高度满屏
查看>>
页面回发速度由 6 秒减少为 0.6 秒的真实案例!
查看>>
一种实现C++反射功能的想法(一)
查看>>
lvs+keepalived+nginx高性能负载均衡集群
查看>>
XXL-Job高可用集群搭建
查看>>
JDBC
查看>>
CodeForces - 123E Maze
查看>>
ZOJ 1709 Oil Deposits(dfs,连通块个数)
查看>>
安卓开源项目周报0308
查看>>