-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path230-KthSmallestElementInABST.go
More file actions
120 lines (108 loc) · 2.73 KB
/
230-KthSmallestElementInABST.go
File metadata and controls
120 lines (108 loc) · 2.73 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
package main
// 230. Kth Smallest Element in a BST
// Given the root of a binary search tree, and an integer k,
// return the kth smallest value (1-indexed) of all the values of the nodes in the tree.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" / >
// 3
// / \
// 1 4
// \
// 2
// Input: root = [3,1,4,null,2], k = 1
// Output: 1
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" / >
// 5
// / \
// 3 6
// / \
// 2 4
// /
// 1
// Input: root = [5,3,6,2,4,null,null,1], k = 3
// Output: 3
// Constraints:
// The number of nodes in the tree is n.
// 1 <= k <= n <= 10^4
// 0 <= Node.val <= 10^4
// Follow up:
// If the BST is modified often (i.e., we can do insert and delete operations)
// and you need to find the kth smallest frequently, how would you optimize?
import "fmt"
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 kthSmallest(root *TreeNode, k int) int {
var inorder func (root *TreeNode ,k, level int) (int,int)
inorder = func (root *TreeNode ,k, level int) (int,int) {
if root == nil {
return -1, level
}
res :=0
res, level = inorder(root.Left, k, level)
if res != -1 {
return res, level
}
if level + 1 == k {
return root.Val, level
}
return inorder(root.Right, k, level + 1)
}
res, _ := inorder(root, k, 0)
return res
}
// stack
func kthSmallest1(root *TreeNode, k int) int {
stack := []*TreeNode{}
for {
for root != nil {
stack = append(stack, root)
root = root.Left
}
stack, root = stack[:len(stack) - 1], stack[len(stack)-1]
k--
if k == 0 {
return root.Val
}
root = root.Right
}
}
func main() {
tree1 := &TreeNode {
3,
&TreeNode {
1,
nil,
&TreeNode{2, nil, nil},
},
&TreeNode{4, nil, nil},
}
fmt.Println(kthSmallest(tree1, 1)) // 1
tree2 := &TreeNode {
5,
&TreeNode {
3,
&TreeNode {
2,
&TreeNode{1, nil, nil},
nil,
},
&TreeNode{4, nil, nil},
},
&TreeNode{6, nil, nil},
}
fmt.Println(kthSmallest(tree2, 3)) // 3
fmt.Println(kthSmallest1(tree1, 1)) // 1
fmt.Println(kthSmallest1(tree2, 3)) // 3
}