-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1602-FindNearestRightNodeInBinaryTree.go
More file actions
156 lines (145 loc) · 4.38 KB
/
1602-FindNearestRightNodeInBinaryTree.go
File metadata and controls
156 lines (145 loc) · 4.38 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
// 1602. Find Nearest Right Node in Binary Tree
// Given the root of a binary tree and a node u in the tree,
// return the nearest node on the same level that is to the right of u,
// or return null if u is the rightmost node in its level.
// Example 1:
// 1
// / \
// 2 3
// \ / \
// (4) 5 6
// <img src="https://assets.leetcode.com/uploads/2020/09/24/p3.png" />
// Input: root = [1,2,3,null,4,5,6], u = 4
// Output: 5
// Explanation: The nearest node on the same level to the right of node 4 is node 5.
// Example 2:
// 3
// \
// 4
// /
// (2)
// <img src="https://assets.leetcode.com/uploads/2020/09/23/p2.png" />
// Input: root = [3,null,4,2], u = 2
// Output: null
// Explanation: There are no nodes to the right of 2.
// Constraints:
// The number of nodes in the tree is in the range [1, 10^5].
// 1 <= Node.val <= 10^5
// All values in the tree are distinct.
// u is a node in the binary tree rooted at root.
import "fmt"
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func findNearestRightNode(root *TreeNode, u *TreeNode) *TreeNode {
// iter to find the target u
height_stack := []int{0}
iter_stack := []*TreeNode{ root }
for len(iter_stack) > 0 {
cur := iter_stack[0]
height := height_stack[0]
if cur.Val == u.Val {
if len(height_stack) == 1 {
return nil
}
next_h := height_stack[1]
if height == next_h {
return iter_stack[1]
}
break
}
iter_stack = iter_stack[1:]
height_stack = height_stack[1:]
if cur.Left != nil {
iter_stack = append(iter_stack, cur.Left)
height_stack = append(height_stack, height+1)
}
if cur.Right != nil {
iter_stack = append(iter_stack, cur.Right)
height_stack = append(height_stack, height+1)
}
}
return nil
}
func findNearestRightNode1(root *TreeNode, u *TreeNode) *TreeNode {
if root == nil {
return nil
}
queue := []*TreeNode{root}
for len(queue) > 0 {
levelSize := len(queue)
for i := 0; i < levelSize; i++ {
curr := queue[0]
queue = queue[1:]
if curr.Val == u.Val {
if i == levelSize - 1 {
return nil
}
return queue[0]
}
if curr.Left != nil {
queue = append(queue, curr.Left)
}
if curr.Right != nil {
queue = append(queue, curr.Right)
}
}
}
return nil
}
func main() {
// Example 1:
// 1
// / \
// 2 3
// \ / \
// (4) 5 6
// <img src="https://assets.leetcode.com/uploads/2020/09/24/p3.png" />
// Input: root = [1,2,3,null,4,5,6], u = 4
// Output: 5
// Explanation: The nearest node on the same level to the right of node 4 is node 5.
tree1 := &TreeNode {
1,
&TreeNode { 2, nil, &TreeNode { 4, nil, nil, }, },
&TreeNode { 3, &TreeNode { 5, nil, nil, }, &TreeNode { 6, nil, nil, }, },
}
fmt.Println(findNearestRightNode(tree1, &TreeNode { 4, nil, nil, })) // &{5 <nil> <nil>}
// Example 2:
// 3
// \
// 4
// /
// (2)
// <img src="https://assets.leetcode.com/uploads/2020/09/23/p2.png" />
// Input: root = [3,null,4,2], u = 2
// Output: null
// Explanation: There are no nodes to the right of 2.
tree2 := &TreeNode {
3,
nil,
&TreeNode { 4, &TreeNode { 2, nil, nil, }, nil, },
}
fmt.Println(findNearestRightNode(tree2, &TreeNode { 2, nil, nil, })) // nil
fmt.Println(findNearestRightNode1(tree1, &TreeNode { 4, nil, nil, })) // &{5 <nil> <nil>}
fmt.Println(findNearestRightNode1(tree2, &TreeNode { 2, nil, nil, })) // nil
}