Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit493065b

Browse files
author
applewjg
committed
Recover Binary Search Tree
Change-Id: I61c2d153edf40d6f992a98f1c0555c90608f8f1a
1 parentc68cc0c commit493065b

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

‎RecoverBinarySearchTree.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 20, 2014
4+
Problem: Recover Binary Search Tree
5+
Difficulty: High
6+
Source: https://oj.leetcode.com/problems/recover-binary-search-tree/
7+
Notes:
8+
Two elements of a binary search tree (BST) are swapped by mistake.
9+
Recover the tree without changing its structure.
10+
Note:
11+
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?
12+
13+
Solution: 1. recursive solution. O(n) space. get inorder list first.
14+
2. recursive solution. O(n) space. with only auxiliary two pointers.
15+
3. Use a stack. Iteration.
16+
4. TBD. Morris inorder traversal. O(1) space. with only auxiliary two pointers.
17+
*/
18+
19+
/**
20+
* Definition for binary tree
21+
* public class TreeNode {
22+
* int val;
23+
* TreeNode left;
24+
* TreeNode right;
25+
* TreeNode(int x) { val = x; }
26+
* }
27+
*/
28+
publicclassSolution {
29+
publicvoidrecoverTree_1(TreeNoderoot) {
30+
if (root ==null)return;
31+
ArrayList<TreeNode>res =newArrayList<TreeNode>();
32+
inorderTraversal(root,res);
33+
TreeNodefirst =null,second =null;
34+
for (inti =1;i <res.size(); ++i) {
35+
if (res.get(i).val >res.get(i-1).val)
36+
continue;
37+
if (first ==null)first =res.get(i-1);
38+
second =res.get(i);
39+
}
40+
if (first ==null)return;
41+
inttmp =first.val;
42+
first.val =second.val;
43+
second.val =tmp;
44+
}
45+
publicvoidinorderTraversal(TreeNoderoot,ArrayList<TreeNode>res) {
46+
if (root ==null)return;
47+
inorderTraversal(root.left,res);
48+
res.add(root);
49+
inorderTraversal(root.right,res);
50+
}
51+
publicvoidrecoverTree_2(TreeNoderoot) {
52+
if (root ==null)return;
53+
TreeNode[]res =newTreeNode[3];// 0->pre, 1->first, 2->second
54+
recoverRe2(root,res);
55+
inttmp =res[1].val;
56+
res[1].val =res[2].val;
57+
res[2].val =tmp;
58+
}
59+
publicvoidrecoverRe2(TreeNoderoot,TreeNode[]res) {
60+
if (root ==null)return;
61+
recoverRe2(root.left,res);
62+
if (res[0] !=null &&res[0].val >root.val) {
63+
if (res[1] ==null)res[1] =res[0];
64+
res[2] =root;
65+
}
66+
res[0] =root;
67+
recoverRe2(root.right,res);
68+
}
69+
70+
publicvoidrecoverTree_3(TreeNoderoot) {
71+
if (root ==null)return;
72+
Stack<TreeNode>stk =newStack<TreeNode>();
73+
TreeNodecur =root,pre =null,first =null,second =null;
74+
while (stk.isEmpty() ==false ||cur !=null) {
75+
if (cur !=null) {
76+
stk.push(cur);
77+
cur =cur.left;
78+
}else {
79+
cur =stk.pop();
80+
if (pre !=null &&pre.val >cur.val) {
81+
if (first ==null)first =pre;
82+
second =cur;
83+
}
84+
pre =cur;
85+
cur =cur.right;
86+
}
87+
}
88+
if (first ==null)return;
89+
inttmp =first.val;
90+
first.val =second.val;
91+
second.val =tmp;
92+
}
93+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp