LeetCode 199 - Binary Tree Right Side View - 题解/Solution

https://leetcode.com/problems/binary-tree-right-side-view/

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 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].
*
* @author dongyuxi
*
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class SolutionTest {
public Solution solution = null;

@Before
public void setup() {
solution = new Solution();
}

@Test
public void test() {
TreeNode node1 = new TreeNode(1);
TreeNode node2 = new TreeNode(2);
TreeNode node3 = new TreeNode(3);
TreeNode node4 = new TreeNode(4);
TreeNode node5 = new TreeNode(5);
node1.left = node2;
node1.right = node3;
node2.right = node5;
node3.right = node4;
assertEquals(Arrays.asList(1, 3, 4), solution.rightSideView(node1));
}
}


支付宝 微信
文章目录