某公司组织架构以二叉搜索树形式记录,节点值为处于该职位的员工编号。请返回第 cnt
大的员工编号。
func findTargetNode(root *TreeNode, cnt int) int {
idx := 0
return dfs(root, &idx, cnt)
}
func dfs(root *TreeNode, idx *int, cnt int) int {
if root == nil {
return -1
}
right := dfs(root.Right, idx, cnt)
(*idx)++
if right != -1 {
return right
}
if *idx == cnt {
return root.Val
}
return dfs(root.Left, idx, cnt)
}