Featured image of post 99. 恢复二叉搜索树

99. 恢复二叉搜索树

题目描述

给你二叉搜索树的根节点 root ,该树中的 恰好 两个节点的值被错误地交换。请在不改变其结构的情况下,恢复这棵树

示例 1:

  • 输入:root = [1,3,null,null,2]
  • 输出:[3,1,null,null,2]
  • 解释:3 不能是 1 的左孩子,因为 3 > 1 。交换 1 和 3 使二叉搜索树有效。

示例 2:

  • 输入:root = [3,1,4,null,null,2]
  • 输出:[2,1,4,null,null,3]
  • 解释:2 不能在 3 的右子树中,因为 2 < 3 。交换 2 和 3 使二叉搜索树有效。

提示:

  • 树上节点的数目在范围 [2, 1000]
  • -231 <= Node.val <= 231 - 1

进阶: 使用 O(n) 空间复杂度的解法很容易实现。你能想出一个只使用 O(1) 空间的解决方案吗?

解法一:显式中序遍历

 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
41
42
43
44
45
46
47
48
49
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func recoverTree(root *TreeNode) {
    var arr []int
    var inorder func(root *TreeNode)
    inorder = func(root *TreeNode) {
        if nil == root {
            return
        }
        inorder(root.Left)
        arr = append(arr, root.Val)
        inorder(root.Right)
    }
    inorder(root)
    a, b := -1, -1
    for i := 0; i < len(arr)-1; i++ {
        if arr[i] > arr[i+1] {
            if a == -1 {
                a = i
            } else {
                b = i + 1
            }
        }
    }
    if b == -1 {
        b = a + 1
    }
    x, y := arr[a], arr[b]
    var fix func(root *TreeNode)
    fix = func(root *TreeNode) {
        if nil == root {
            return
        }
        if root.Val == x {
            root.Val =y
        } else if root.Val == y {
            root.Val = x
        }
        fix(root.Left)
        fix(root.Right)
    }
    fix(root)
}

a == -1 时,可以直接更新 b = a + 1,修改后的代码如下:

 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
41
42
43
44
45
46
47
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func recoverTree(root *TreeNode) {
    var arr []int
    var inorder func(root *TreeNode)
    inorder = func(root *TreeNode) {
        if nil == root {
            return
        }
        inorder(root.Left)
        arr = append(arr, root.Val)
        inorder(root.Right)
    }
    inorder(root)
    a, b := -1, -1
    for i := 0; i < len(arr)-1; i++ {
        if arr[i] > arr[i+1] {
            if a == -1 {
                a = i
                b = a + 1// 直接更新 b
            } else {
                b = i + 1
            }
        }
    }
    x, y := arr[a], arr[b]
    var fix func(root *TreeNode)
    fix = func(root *TreeNode) {
        if nil == root {
            return
        }
        if root.Val == x {
            root.Val =y
        } else if root.Val == y {
            root.Val = x
        }
        fix(root.Left)
        fix(root.Right)
    }
    fix(root)
}

解法二:隐式中序遍历

 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
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func recoverTree(root *TreeNode) {
    var prev, nodeA, nodeB *TreeNode
    var inorder func(root *TreeNode)
    inorder = func(root *TreeNode) {
        if nil == root {
            return
        }
        inorder(root.Left)
        if nil != prev {
            if prev.Val > root.Val {
                if nil == nodeA {
                    nodeA = prev
                    nodeB = root
                } else {
                    nodeB = root
                }
            }
        }
        prev = root
        inorder(root.Right)
    }
    inorder(root)
    nodeA.Val, nodeB.Val = nodeB.Val, nodeA.Val
}

使用迭代法的代码如下:

解法三:Morris 中序遍历

 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func recoverTree(root *TreeNode) {
    var prev, nodeA, nodeB *TreeNode
    cur := root
    for nil != cur {
        if nil != cur.Left {
            mostRight := cur.Left
            for nil != mostRight.Right && cur != mostRight.Right {
                mostRight = mostRight.Right
            }
            if nil == mostRight.Right {
                mostRight.Right= cur
                cur = cur.Left
                continue
            } else {
                // cur == mostRight.Right
                mostRight.Right = nil
                if nil != prev {
                    if prev.Val > cur.Val {
                        if nil == nodeA {
                            nodeA = prev
                            nodeB = cur
                        } else {
                            nodeB = cur
                        }
                    }
                }
                prev = cur
                // 已经遍历完左子树,转到右子树
                cur = cur.Right
            }
        } else {
            if nil != prev {
                if prev.Val > cur.Val {
                    if nil ==  nodeA {
                        nodeA = prev
                        nodeB = cur
                    } else {
                        nodeB = cur
                    }
                }
            }
            prev = cur
            // 转到右子树
            cur = cur.Right
        }
    }
    nodeA.Val, nodeB.Val = nodeB.Val, nodeA.Val
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2023/08/03 18:41:36
comments powered by Disqus
Built with Hugo
主题 StackJimmy 设计