Skip to content

Latest commit

 

History

History
22 lines (18 loc) · 374 Bytes

面试题54. 二叉搜索树的第k大节点.md

File metadata and controls

22 lines (18 loc) · 374 Bytes
func kthLargest(root *TreeNode, k int) int {
   var nums []int
   nums = getNums(&nums, root)
   return nums[k-1]
}

func getNums(nums *[]int, root *TreeNode) []int {
   if root.Right != nil {
      getNums(nums, root.Right)
   }
   
   if root != nil {
      *nums = append(*nums, root.Val)
   }
   
   if root.Left != nil{
      getNums(nums, root.Left)
   }
}