Skip to content

Commit ed8f1a6

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0897
1 parent 0554c33 commit ed8f1a6

File tree

6 files changed

+172
-4
lines changed

6 files changed

+172
-4
lines changed

lcci/17.12.BiNode/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
递归将左子树、右子树转换为左、右链表 left 和 right。然后将左链表 left 的最后一个结点的 right 指针指向 root,root 的 right 指针指向右链表 right,并将 root 的 left 指针值为空。
3333

34+
[897. 递增顺序查找树](/solution/0800-0899/0897.Increasing%20Order%20Search%20Tree/README.md)
35+
3436
<!-- tabs:start -->
3537

3638
### **Python3**

lcci/17.12.BiNode/README_EN.md

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
## Solutions
3232

33+
See [897. Increasing Order Search Tree](/solution/0800-0899/0897.Increasing%20Order%20Search%20Tree/README_EN.md).
34+
3335
<!-- tabs:start -->
3436

3537
### **Python3**

solution/0800-0899/0897.Increasing Order Search Tree/README.md

+58-2
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,78 @@
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
递归将左子树、右子树转换为左、右链表 left 和 right。然后将左链表 left 的最后一个结点的 right 指针指向 root,root 的 right 指针指向右链表 right,并将 root 的 left 指针值为空。
58+
59+
[面试题 17.12. BiNode](/lcci/17.12.BiNode/README.md)
60+
5761
<!-- tabs:start -->
5862

5963
### **Python3**
6064

6165
<!-- 这里可写当前语言的特殊实现逻辑 -->
6266

6367
```python
64-
68+
# Definition for a binary tree node.
69+
# class TreeNode:
70+
# def __init__(self, val=0, left=None, right=None):
71+
# self.val = val
72+
# self.left = left
73+
# self.right = right
74+
class Solution:
75+
def increasingBST(self, root: TreeNode) -> TreeNode:
76+
if root is None:
77+
return None
78+
left = self.increasingBST(root.left)
79+
right = self.increasingBST(root.right)
80+
if left is None:
81+
root.right = right
82+
return root
83+
res = left
84+
while left and left.right:
85+
left = left.right
86+
left.right = root
87+
root.right = right
88+
root.left = None
89+
return res
6590
```
6691

6792
### **Java**
6893

6994
<!-- 这里可写当前语言的特殊实现逻辑 -->
7095

7196
```java
72-
97+
/**
98+
* Definition for a binary tree node.
99+
* public class TreeNode {
100+
* int val;
101+
* TreeNode left;
102+
* TreeNode right;
103+
* TreeNode() {}
104+
* TreeNode(int val) { this.val = val; }
105+
* TreeNode(int val, TreeNode left, TreeNode right) {
106+
* this.val = val;
107+
* this.left = left;
108+
* this.right = right;
109+
* }
110+
* }
111+
*/
112+
class Solution {
113+
public TreeNode increasingBST(TreeNode root) {
114+
if (root == null) return null;
115+
TreeNode left = increasingBST(root.left);
116+
TreeNode right = increasingBST(root.right);
117+
if (left == null) {
118+
root.right = right;
119+
return root;
120+
}
121+
TreeNode res = left;
122+
while (left != null && left.right != null) left = left.right;
123+
left.right = root;
124+
root.right = right;
125+
root.left = null;
126+
return res;
127+
}
128+
}
73129
```
74130

75131
### **...**

solution/0800-0899/0897.Increasing Order Search Tree/README_EN.md

+56-2
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,72 @@
4747

4848
## Solutions
4949

50+
See [17.12. BiNode](/lcci/17.12.BiNode/README_EN.md).
51+
5052
<!-- tabs:start -->
5153

5254
### **Python3**
5355

5456
```python
55-
57+
# Definition for a binary tree node.
58+
# class TreeNode:
59+
# def __init__(self, val=0, left=None, right=None):
60+
# self.val = val
61+
# self.left = left
62+
# self.right = right
63+
class Solution:
64+
def increasingBST(self, root: TreeNode) -> TreeNode:
65+
if root is None:
66+
return None
67+
left = self.increasingBST(root.left)
68+
right = self.increasingBST(root.right)
69+
if left is None:
70+
root.right = right
71+
return root
72+
res = left
73+
while left and left.right:
74+
left = left.right
75+
left.right = root
76+
root.right = right
77+
root.left = None
78+
return res
5679
```
5780

5881
### **Java**
5982

6083
```java
61-
84+
/**
85+
* Definition for a binary tree node.
86+
* public class TreeNode {
87+
* int val;
88+
* TreeNode left;
89+
* TreeNode right;
90+
* TreeNode() {}
91+
* TreeNode(int val) { this.val = val; }
92+
* TreeNode(int val, TreeNode left, TreeNode right) {
93+
* this.val = val;
94+
* this.left = left;
95+
* this.right = right;
96+
* }
97+
* }
98+
*/
99+
class Solution {
100+
public TreeNode increasingBST(TreeNode root) {
101+
if (root == null) return null;
102+
TreeNode left = increasingBST(root.left);
103+
TreeNode right = increasingBST(root.right);
104+
if (left == null) {
105+
root.right = right;
106+
return root;
107+
}
108+
TreeNode res = left;
109+
while (left != null && left.right != null) left = left.right;
110+
left.right = root;
111+
root.right = right;
112+
root.left = null;
113+
return res;
114+
}
115+
}
62116
```
63117

64118
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public TreeNode increasingBST(TreeNode root) {
18+
if (root == null) return null;
19+
TreeNode left = increasingBST(root.left);
20+
TreeNode right = increasingBST(root.right);
21+
if (left == null) {
22+
root.right = right;
23+
return root;
24+
}
25+
TreeNode res = left;
26+
while (left != null && left.right != null) left = left.right;
27+
left.right = root;
28+
root.right = right;
29+
root.left = null;
30+
return res;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def increasingBST(self, root: TreeNode) -> TreeNode:
9+
if root is None:
10+
return None
11+
left = self.increasingBST(root.left)
12+
right = self.increasingBST(root.right)
13+
if left is None:
14+
root.right = right
15+
return root
16+
res = left
17+
while left and left.right:
18+
left = left.right
19+
left.right = root
20+
root.right = right
21+
root.left = None
22+
return res

0 commit comments

Comments
 (0)