题解 | #找到搜索二叉树中两个错误的节点#
找到搜索二叉树中两个错误的节点
https://www.nowcoder.com/practice/4582efa5ffe949cc80c136eeb78795d6
func findError( root *TreeNode ) []int { // write code here var pre *TreeNode ans := make([]int, 2) idx := 1 var dfs func(cur *TreeNode) dfs = func(cur *TreeNode) { if cur==nil{ return } dfs(cur.Left) if pre==nil{ pre = cur } if idx==1&&cur.Val<pre.Val{ ans[idx] = pre.Val idx-- } if idx==0&&cur.Val<pre.Val{ ans[idx] = cur.Val } pre = cur dfs(cur.Right) } dfs(root) return ans }