mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-25 12:14:26 +08:00
36 lines
614 B
Go
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)
|
|
}
|
|
}
|