-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeRightSideView.java
More file actions
47 lines (43 loc) · 1.1 KB
/
BinaryTreeRightSideView.java
File metadata and controls
47 lines (43 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 题目:返回二叉树的最右侧视图
* 例如:
* Given the following binary tree,
* 1 <---
* / \
* 2 3 <---
* \ \
* 5 4 <---
* You should return [1, 3, 4].
* 解题思路:利用层次遍历的方法,每次将该层的最后一个元素加入到结果集中
*
*/
import java.util.*;
public class BinaryTreeRightSideView {
}
class Solution137
{
public ArrayList<Integer> rightSideView(TreeNode root)
{
ArrayList<Integer> result=new ArrayList<Integer>();
if(root==null) return result;
ArrayDeque<TreeNode> Q=new ArrayDeque<TreeNode>();
Q.add(root);
while(!Q.isEmpty())
{
//二叉树中每一行的节点个数
int Size=Q.size();
for(int i=0;i<Size-1;i++)
{
TreeNode node=Q.poll();
if(node.left!=null) Q.add(node.left);
if(node.right!=null) Q.add(node.right);
}
//把最后一个节点加入到队列中及结果集中
TreeNode lastNode=Q.poll();
result.add(lastNode.val);
if(lastNode.left!=null) Q.add(lastNode.left);
if(lastNode.right!=null) Q.add(lastNode.right);
}
return result;
}
}