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
| /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ var index int func kthLargest(root *TreeNode, k int) int { if root == nil k < 1{ return -1 } index = 0 node := convertToSearch(root, k) fmt.Println("====node:" , node) return node.Val }
func convertToSearch(node *TreeNode, k int) *TreeNode{ if node.Right != nil{ right := convertToSearch(node.Right, k) if right != nil{ return right } }
index ++ fmt.Println("index:" , index) if index == k{ return node }
if node.Left != nil { left := convertToSearch(node.Left, k) if left != nil { return left } }
|