-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode_199_BinaryTreeRightSideView.java
More file actions
42 lines (37 loc) · 1.09 KB
/
Leetcode_199_BinaryTreeRightSideView.java
File metadata and controls
42 lines (37 loc) · 1.09 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author KD
*
* 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.
Example:
Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation:
1 <---
/ \
2 3 <---
\ \
5 4 <---
*
*/
import java.util.*;
public class Leetcode_199_BinaryTreeRightSideView {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> list = new ArrayList<>();
rightSideView(root, list, 0);
return list;
}
public void rightSideView(TreeNode root, List<Integer> list, int height) {
if (root == null) return;
if (height < list.size()) list.set(height, root.val);
else list.add(root.val);
rightSideView(root.left, list, height+1);
rightSideView(root.right, list, height+1);
}
}