199. 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.
For example:
Given the following binary tree,
1 <---
/ \
2 3 <---
\ \
5 4 <---
You should return [1, 3, 4]
.
代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> rightSideView(TreeNode root) {
boolean flag=true;
List<Integer> list=new ArrayList<>();
ArrayList<TreeNode> res=new ArrayList<TreeNode>();
Map<Integer,Integer> map=new HashMap<>(); if(root==null)
return list; res=LevelTraverse(root);
list.add(root.val);
map.put(0,0); while(flag)
{
while(root.right!=null)
{
root=root.right;
list.add(root.val);
map.put(res.indexOf(root),res.indexOf(root)); }
if(root.left!=null)
{
root=root.left;
list.add(root.val);
map.put(res.indexOf(root),res.indexOf(root));
}
else{
if(res.indexOf(root)-1<0)
flag=false;
else {
root=res.get(res.indexOf(root)-1);
if(map.containsKey(res.indexOf(root)))
break;
}
}
}
return list;
} public ArrayList<TreeNode> LevelTraverse(TreeNode root)//树的水平遍历
{
ArrayList<TreeNode> list=new ArrayList<TreeNode>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while(queue.size()>0)
{
TreeNode a=(TreeNode)queue.peek();
queue.remove();
list.add(a);
if(a.left!=null)
queue.add(a.left);
if(a.right!=null)
queue.add(a.right);
} return list;
}
}
199. Binary Tree Right Side View的更多相关文章
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【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, ...
- 【刷题-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, ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
随机推荐
- NSString length的坑。
说坑,可能过头了,是我理所当然的把OC看作C了, char* cstr = "zh中文12"; NSString* s = [NSString stringWithUTF8Stri ...
- HDU 4046 Panda
线段树单点更新,要注意两段合并多出的答案的计算即可 //======================================================================== ...
- 戴文的Linux内核专题:08内核配置(5)
转自Linux中国 Linux内核拥有许多可以配置的特性,接下来我们还有许多要配置. 下一个可以配置的特性是x86的随机数生成器(x86 architectural random number gen ...
- Maven 玩 github上的项目
第一步,使用maven创建了一个项目"helloworld",cmd命令如下: @echo offecho [INFO] Generating project in ./gener ...
- 计算C++类所占用的字节(即sizeof)
在类中,如果什么都没有,则类占用1个字节,一旦类中有其他的占用空间成员,则这1个字节就不在计算之内,如一个类只有一个int则占用4字节而不是5字节.如果只有成员函数,则还是只占用1个字节,因为类函数不 ...
- JAVA每日一旅3
1.关于byte byte在内存中占一个字节,范围是-128-127,128作强制类型转换到byte变成-128,因为128的二进制表示:1000 0000,最高位是符号位. 2.关于Hibernat ...
- vs2013的使用和单元测试
我的vs2013是之前就安装好的,安装过程就不介绍了,我平常编写代码就是用的vs2013,用起来还是很方便的,现在我们就开始使用vs2013进行单元测试 首先我们建立一个项目,项目中选择virtual ...
- poj2129 dp
//Accepted 320 KB 47 ms //dp //dp[i][j]=1 表示用s1的前i个,s2的前j个字符能构成s3的前i+j-1个字符 //dp[i][j]=0 表示构不成 //dp[ ...
- jQuery之load、unload、onunload和onbeforeunload
1.load:jQuery load() 方法是简单但强大的 AJAX 方法.load() 方法从服务器加载数据,并把返回的数据放入被选元素中. 语法:$(selector).load(URL,dat ...
- hadoop 中对Vlong 和 Vint的压缩方法
hadoop 中对java的基本类型进行了writeable的封装,并且所有这些writeable都是继承自WritableComparable的,都是可比较的:并且,它们都有对应的get() 和 s ...