[LeetCode] Recover Binary Search Tree

简介: As the note in the problem statement, this problem has a straight-forward O(n)-space solution, which is to generate the inorder traversal results of t...

As the note in the problem statement, this problem has a straight-forward O(n)-space solution, which is to generate the inorder traversal results of the bst and then find the two nodes that violate the increasing trend and those are the two that requires to be swapped.

This link utilizes inorder traversal to find those two nodes without keeping all the results. However, inorder traversal implemented recursively will take at least O(logn) space and may even take O(n) space at the worst case. The code is rewritten as follows.

 1 class Solution {
 2 public:
 3     void recoverTree(TreeNode* root) {
 4         pre = first = second = NULL;
 5         inorder(root);
 6         if (first) {
 7             int temp = first -> val;
 8             first -> val = second -> val;
 9             second -> val = temp;
10         }
11     }
12 private:
13     TreeNode *first, *second, *pre;
14     void inorder(TreeNode* root) {
15         if (!root) return;
16         inorder(root -> left);
17         if (pre && pre -> val > root -> val) {
18             if (!first) first = pre;
19             second = root;
20         }
21         pre = root;
22         inorder(root -> right);
23     }
24 };

So to come up with an O(1)-space solution, we indeed have to turn to Morris traversal. This link has a nice explanation of how Morris traversal can be modified to find the two wrong nodes.

 1 class Solution {
 2 public:
 3     void recoverTree(TreeNode* root) {
 4         TreeNode* cur = root;
 5         TreeNode *first, *second, *pre;
 6         first = second = pre = NULL;
 7         while (cur) {
 8             if (cur -> left) {
 9                 TreeNode* predecessor = cur -> left;
10                 while (predecessor -> right && predecessor -> right != cur)
11                     predecessor = predecessor -> right;
12                 if (!(predecessor -> right)) {
13                     predecessor -> right = cur;
14                     cur = cur -> left;
15                 }
16                 else {
17                     predecessor -> right = NULL;
18                     if (pre && pre -> val > cur -> val) {
19                         if (!first) first = pre;
20                         second = cur;
21                     }
22                     pre = cur;
23                     cur = cur -> right;
24                 }
25             }
26             else {
27                 if (pre && pre -> val > cur -> val) {
28                     if (!first) first = pre;
29                     second = cur;
30                 }
31                 pre = cur;
32                 cur = cur -> right;
33             }
34         }
35         if (first) swap(first -> val, second -> val);
36     }
37 };

 

目录
相关文章
|
11月前
|
Java
Leetcode 114. Flatten Binary Tree to Linked List
思路也很简单,先把root的左子树(如有)变成单链表 leftlinkedlist,把root的右子树(如有)变成单链表 rightlinkedlist,再把root的右节点变成leftlikedlist,再把rightlinkedlist接到leftlinkedlist后面,代码如下。
36 1
|
11月前
Leetcode 74. Search a 2D Matrix
这道题很简单,为此专门写篇博客其实算博客凑数了。给你一个每一行每一列都是增序,且每一行第一个数都大于上一行末尾数的矩阵,让你判断某个数在这个矩阵中是否存在。 假设矩阵是m*n,扫一遍的时间复杂度就是O(m*n),题目中给出的这么特殊的矩阵,时间复杂度可以降到O(m+n),具体代码如下,写的比较挫。
79 1
|
11月前
Leetcode Minimum Depth of Binary Tree (面试题推荐)
计算树的最小深度 很简单的一道题,只需要遍历一次树,到叶子节点的时候计算一下深度和当前最小深度比较,保存最小值就行。 我在这用了一个全局变量 mindepth。总感觉我这代码写的不够简练,求更精简的方法。
43 0
|
11月前
Leetcode Binary Tree Postorder Traversal(面试题推荐)
非递后续归遍历二叉树,肯定得用到栈。先序遍历很好写,但后续遍历就不是那么容易了。 只需要设置个指针pre,指向最后输出的那个节点就行了,只要判断cur指针指向的是上次输出节点的父节点,且cur无其他未遍历的节点,这个时候就把cur节点输出即可,然后更改pre。原理是要遍历当前节点,其所有子节点都必须遍历完,因为肯定是先左后右,所以只需一个指针保持前一次输出的结果即可。
47 0
|
11月前
Leetcode 236. Lowest Common Ancestor of a Binary Tree
根据LCA的定义,二叉树中最小公共祖先就是两个节点p和q最近的共同祖先节点,LCA的定义没什么好解释的,主要是这道题的解法。
32 0
|
11月前
Leetcode 623. Add One Row to Tree
题目很简单,在树的第d层加一层,值为v。递归增加一层就好了。代码如下
41 0
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
LeetCode Contest 178-1367. 二叉树中的列表 Linked List in Binary Tree
LeetCode Contest 178-1367. 二叉树中的列表 Linked List in Binary Tree
|
算法 Python
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 102. 二叉树的层序遍历 Binary Tree Level Order Traversal
LeetCode 102. 二叉树的层序遍历 Binary Tree Level Order Traversal