add new solutions
This commit is contained in:
parent
9c2c959a9b
commit
9a10695e8c
29 changed files with 1074 additions and 2 deletions
35
solutions/2/q230/solution.go
Normal file
35
solutions/2/q230/solution.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package q230
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func findKth(node *TreeNode, k, current int) (int, *int) {
|
||||
if node == nil {
|
||||
return current, nil
|
||||
}
|
||||
|
||||
current, ret := findKth(node.Left, k, current)
|
||||
if ret != nil {
|
||||
return current, ret
|
||||
}
|
||||
|
||||
current += 1
|
||||
if current == k {
|
||||
return current, &node.Val
|
||||
}
|
||||
|
||||
return findKth(node.Right, k, current)
|
||||
}
|
||||
|
||||
func kthSmallest(root *TreeNode, k int) int {
|
||||
_, kth := findKth(root, k, 0)
|
||||
if kth == nil {
|
||||
return 0
|
||||
}
|
||||
return *kth
|
||||
}
|
||||
|
||||
var _ = kthSmallest
|
||||
Loading…
Add table
Add a link
Reference in a new issue