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?
[分析] 正常的BST,其中序遍历序列是一个递增序列,假设A,B节点互换了位置,则在中序遍历中A比其后元素大,B比前面元素小,可据此性质找出AB节点并调换回来。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
// Method 2: recursive
TreeNode prev, p, q;
public void recoverTree(TreeNode root) {
inorder(root);
if (p != null) {
int tmp = p.val;
p.val = q.val;
q.val = tmp;
}
}
public void inorder(TreeNode root) {
if (root == null) return;
inorder(root.left);
if (prev != null && prev.val > root.val) {
if (p == null)
p = prev;
q = root;
}
prev = root;
inorder(root.right);
}
// Method 1: iterative
public void recoverTree1(TreeNode root) {
if (root == null) return;
LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
TreeNode curr = root, prev = null;
TreeNode errPair1First = null, errPair1Sec = null;
TreeNode errPair2Sec = null;
while (!stack.isEmpty() || curr != null) {
if (curr != null) {
stack.push(curr);
curr = curr.left;
} else {
curr = stack.pop();
if (prev != null && prev.val > curr.val) {
if (errPair1First == null) {
errPair1First = prev;
errPair1Sec = curr;
} else {
errPair2Sec = curr;
break;
}
}
prev = curr;
curr = curr.right;
}
}
if (errPair2Sec == null) {
swap(errPair1First, errPair1Sec);
} else {
swap(errPair1First, errPair2Sec);
}
}
private void swap (TreeNode node1, TreeNode node2) {
int tmp = node1.val;
node1.val = node2.val;
node2.val = tmp;
}
}
分享到:
相关推荐
java java_leetcode-99-recover-binary-search-tree
js js_leetcode题解之99-recover-binary-search-tree.js
python python_leetcode题解之099_Recover_Binary_Search_Tree
c语言基础 c语言_leetcode题解之0099_recover_binary_search_tree.zip
* [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search]...
- **Recover Binary Search Tree**:恢复二叉搜索树中的两个错误节点。 - **Binary Tree Path**:找到二叉树中和为目标值的路径。 - **Sum Root to Leaf Numbers**:计算二叉树所有从根到叶子节点的路径之和。 4...
lru缓存leetcode leetcode 大批 41. First Missing Positive 广度优先搜索 ...Recover Binary Search Tree 109. Convert Sorted List to Binary Search Tree 116. Populating Next Right Pointers in Each No
Recover Binary Search Tree - Java Recursive 0101 Symmetric tree - Java Recursive - Java Iterative - C Recursive - Python Iterative 0102 Binary Tree Level Order Traversal - Python3 iterative 0103...
- BinaryTree(二叉树) - HuffmanCompression(霍夫曼编码) - Queue(队列) - Heap(堆) - Stack(栈) - Set(集合) - Map(映射/哈希表) - Graph(图) - **Basics Sorting**(基本排序算法) - ...
本篇内容将深入解析Python在LeetCode面试中的第99题——恢复二叉搜索树(Recover Binary Search Tree)。 二叉搜索树(Binary Search Tree,BST)是一种特殊的二叉树,它的每个节点都满足以下性质:左子树上所有...
Recover a Tree From Preorder Traversal Hard (73.62 %) [1027] Longest Arithmetic Sequence Medium (41.26 %) [1026] Maximum Difference Between Node and Ancestor Medium (55.48 %) [1025] Divisor Game Easy ...
接着,我们来看第9题,"恢复二叉搜索树的顺序"( Recover Binary Search Tree)。这道题要求恢复一棵二叉搜索树中两个节点的值,使得树重新符合二叉搜索树的性质。二叉搜索树的特点是左子树上的所有节点值小于父节点...