-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path199.binary-tree-right-side-view.go
95 lines (86 loc) · 1.8 KB
/
199.binary-tree-right-side-view.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* @lc app=leetcode id=199 lang=golang
*
* [199] Binary Tree Right Side View
*
* https://leetcode.com/problems/binary-tree-right-side-view/description/
*
* algorithms
* Medium (53.36%)
* Likes: 2445
* Dislikes: 142
* Total Accepted: 306.1K
* Total Submissions: 565.3K
* Testcase Example: '[1,2,3,null,5,null,4]'
*
* 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 <---
*
*/
// @lc code=start
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func rightSideView(root *TreeNode) []int {
return rightSideView1(root)
}
// [1,2,3,4]
func rightSideView2(root *TreeNode) []int {
if root == nil {
return []int{}
}
retVal, stack := []int{}, []*TreeNode{root}
for len(stack) > 0 {
lastNode := stack[len(stack)-1]
retVal = append(retVal, lastNode.Val)
nextStack := []*TreeNode{}
for i := 0; i < len(stack); i++ {
curNode := stack[i]
if curNode.Left != nil {
nextStack = append(nextStack, curNode.Left)
}
if curNode.Right != nil {
nextStack = append(nextStack, curNode.Right)
}
}
stack = nextStack
}
return retVal
}
func rightSideView1(root *TreeNode) []int {
if root == nil {
return nil
}
retVal := []int{}
helper(root, &retVal, 0)
return retVal
}
func helper(root *TreeNode, retVal *[]int, depth int) {
if root == nil {
return
}
if len(*retVal) == depth {
*retVal = append(*retVal, root.Val)
}
helper(root.Right, retVal, depth+1)
helper(root.Left, retVal, depth+1)
}
// @lc code=end