Files
LeetCode-Go/leetcode/0230.Kth-Smallest-Element-in-a-BST/230. Kth Smallest Element in a BST.go
2020-08-07 17:06:53 +08:00

36 lines
614 B
Go

package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func kthSmallest(root *TreeNode, k int) int {
res, count := 0, 0
inorder230(root, k, &count, &res)
return res
}
func inorder230(node *TreeNode, k int, count *int, ans *int) {
if node != nil {
inorder230(node.Left, k, count, ans)
*count++
if *count == k {
*ans = node.Val
return
}
inorder230(node.Right, k, count, ans)
}
}