zoukankan      html  css  js  c++  java
  • [leetcode] Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake.

    Recover the tree without changing its structure.

    Note:
    A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?

    https://oj.leetcode.com/problems/recover-binary-search-tree/

    思路1:中序遍历生成序列,然后对其中错序的进行调整。 需要额外O(n)的空间。

    思路2:中序遍历二叉树,记录当前指针cur的前一个节点pre,如果pre.val大于cur.val,表示有错序,多数情况错序有两次;如果有一次错序,说明就是相邻节点需要被交换。

    public class Solution {
    
        private TreeNode pre;
        private TreeNode wrong1;
        private TreeNode wrong2;
    
        public void recoverTree(TreeNode root) {
            preOrder(root);
            int tmp = wrong1.val;
            wrong1.val = wrong2.val;
            wrong2.val = tmp;
    
        }
    
        private void preOrder(TreeNode root) {
            if (root == null)
                return;
            preOrder(root.left);
    
            if (pre != null && pre.val > root.val) {
                if (wrong1 == null) {
                    wrong1 = pre;
                    wrong2 = root;
                } else {
                    wrong2 = root;
                }
    
            }
    
            pre = root;
    
            preOrder(root.right);
    
        }
    
        public static void main(String[] args) {
            TreeNode root = new TreeNode(10);
            root.left = new TreeNode(5);
            root.right = new TreeNode(15);
            root.left.right = new TreeNode(13);
            root.right.left = new TreeNode(7);
    
            new Solution().recoverTree(root);
    
        }
    
    }
    View Code

    第二遍记录:注意记录pre的用法。

    第三遍记录:

      举一个例子,中序遍历后正常应该是递增的,随意交换两个元素,会发现两种情况

      交换相邻元素后,序列只有一处地方递减。

      交换非相邻元素,序列有两处地方递减。

    public class Solution {
    
        private TreeNode one;
        private TreeNode two;
        private TreeNode pre;
    
        public void recoverTree(TreeNode root) {
            inorder(root);
            int tmp = one.val;
            one.val = two.val;
            two.val = tmp;
    
        }
    
        private void inorder(TreeNode root) {
            if (root != null) {
                inorder(root.left);
    
                if (pre != null && pre.val > root.val) {
                    if (one == null) {
                        one = pre;
                        two = root;
                    } else {
                        two = root;
                    }
                }
    
                pre = root;
                inorder(root.right);
            }
        }
    
        public static void main(String[] args) {
            TreeNode root = new TreeNode(10);
            root.left = new TreeNode(5);
            root.right = new TreeNode(15);
            root.left.right = new TreeNode(13);
            root.right.left = new TreeNode(7);
    
            new Solution().recoverTree(root);
    
        }
    
    }

    参考:

    http://blog.csdn.net/worldwindjp/article/details/21694179

  • 相关阅读:
    你应该了解Nginx的7个原因
    linux 中php以及nginx的重启命令
    HTTP 长连接和短连接
    git常用命令
    Linux 防火墙开放特定端口 (iptables)
    redis配置文件相关
    关于解决emoji表情的存储
    文件内容统计——Linux wc命令
    Git 服务器搭建
    关于微信二次分享,描述变链接的解决方法(一)----文档说明
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3821290.html
Copyright © 2011-2022 走看看