Skip to content

Commit c1ed3bb

Browse files
committed
feat: add solutions to lc/lcof2 problem: Inorder Successor in BST
1 parent 92aa7f4 commit c1ed3bb

File tree

11 files changed

+454
-6
lines changed

11 files changed

+454
-6
lines changed

lcof2/剑指 Offer II 053. 二叉搜索树中的中序后继/README.md

+97-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<p><strong>示例 1:</strong></p>
1414

15-
<p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20053.%20%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E4%B8%AD%E5%BA%8F%E5%90%8E%E7%BB%A7/images/285_example_1.PNG" style="height: 117px; width: 122px;" /></p>
15+
<p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20053.%20%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E4%B8%AD%E5%BA%8F%E5%90%8E%E7%BB%A7/images/285_example_1.png" style="height: 117px; width: 122px;" /></p>
1616

1717
<pre>
1818
<strong>输入:</strong>root = [2,1,3], p = 1
@@ -22,7 +22,7 @@
2222

2323
<p><strong>示例&nbsp;2:</strong></p>
2424

25-
<p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20053.%20%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E4%B8%AD%E5%BA%8F%E5%90%8E%E7%BB%A7/images/285_example_2.PNG" style="height: 229px; width: 246px;" /></p>
25+
<p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20053.%20%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E4%B8%AD%E5%BA%8F%E5%90%8E%E7%BB%A7/images/285_example_2.png" style="height: 229px; width: 246px;" /></p>
2626

2727
<pre>
2828
<strong>输入:</strong>root = [5,3,6,2,4,null,null,1], p = 6
@@ -49,22 +49,116 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
利用二叉搜索树的特性,`p` 的中序后继一定是所有大于 `p` 的节点中最小的那个
53+
5254
<!-- tabs:start -->
5355

5456
### **Python3**
5557

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

5860
```python
59-
61+
# Definition for a binary tree node.
62+
# class TreeNode:
63+
# def __init__(self, x):
64+
# self.val = x
65+
# self.left = None
66+
# self.right = None
67+
68+
class Solution:
69+
def inorderSuccessor(self, root: 'TreeNode', p: 'TreeNode') -> 'TreeNode':
70+
cur, ans = root, None
71+
while cur:
72+
if cur.val <= p.val:
73+
cur = cur.right
74+
else:
75+
ans = cur
76+
cur = cur.left
77+
return ans
6078
```
6179

6280
### **Java**
6381

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

6684
```java
85+
/**
86+
* Definition for a binary tree node.
87+
* public class TreeNode {
88+
* int val;
89+
* TreeNode left;
90+
* TreeNode right;
91+
* TreeNode(int x) { val = x; }
92+
* }
93+
*/
94+
class Solution {
95+
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
96+
TreeNode cur = root, ans = null;
97+
while (cur != null) {
98+
if (cur.val <= p.val) {
99+
cur = cur.right;
100+
} else {
101+
ans = cur;
102+
cur = cur.left;
103+
}
104+
}
105+
return ans;
106+
}
107+
}
108+
```
109+
110+
### **Go**
111+
112+
```go
113+
/**
114+
* Definition for a binary tree node.
115+
* type TreeNode struct {
116+
* Val int
117+
* Left *TreeNode
118+
* Right *TreeNode
119+
* }
120+
*/
121+
func inorderSuccessor(root *TreeNode, p *TreeNode) (ans *TreeNode) {
122+
cur := root
123+
for cur != nil {
124+
if cur.Val <= p.Val {
125+
cur = cur.Right
126+
} else {
127+
ans = cur
128+
cur = cur.Left
129+
}
130+
}
131+
return
132+
}
133+
```
67134

135+
### **C++**
136+
137+
```cpp
138+
/**
139+
* Definition for a binary tree node.
140+
* struct TreeNode {
141+
* int val;
142+
* TreeNode *left;
143+
* TreeNode *right;
144+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
145+
* };
146+
*/
147+
class Solution {
148+
public:
149+
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
150+
TreeNode *cur = root, *ans = nullptr;
151+
while (cur != nullptr) {
152+
if (cur->val <= p->val) {
153+
cur = cur->right;
154+
} else {
155+
ans = cur;
156+
cur = cur->left;
157+
}
158+
}
159+
return ans;
160+
}
161+
};
68162
```
69163
70164
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
13+
TreeNode *cur = root, *ans = nullptr;
14+
while (cur != nullptr) {
15+
if (cur->val <= p->val) {
16+
cur = cur->right;
17+
} else {
18+
ans = cur;
19+
cur = cur->left;
20+
}
21+
}
22+
return ans;
23+
}
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func inorderSuccessor(root *TreeNode, p *TreeNode) (ans *TreeNode) {
10+
cur := root
11+
for cur != nil {
12+
if cur.Val <= p.Val {
13+
cur = cur.Right
14+
} else {
15+
ans = cur
16+
cur = cur.Left
17+
}
18+
}
19+
return
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
12+
TreeNode cur = root, ans = null;
13+
while (cur != null) {
14+
if (cur.val <= p.val) {
15+
cur = cur.right;
16+
} else {
17+
ans = cur;
18+
cur = cur.left;
19+
}
20+
}
21+
return ans;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def inorderSuccessor(self, root: 'TreeNode', p: 'TreeNode') -> 'TreeNode':
10+
cur, ans = root, None
11+
while cur:
12+
if cur.val <= p.val:
13+
cur = cur.right
14+
else:
15+
ans = cur
16+
cur = cur.left
17+
return ans

solution/0200-0299/0285.Inorder Successor in BST/README.md

+96-2
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,118 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
利用二叉搜索树的特性,`p` 的中序后继一定是所有大于 `p` 的节点中最小的那个
51+
5052
<!-- tabs:start -->
5153

5254
### **Python3**
5355

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

5658
```python
57-
59+
# Definition for a binary tree node.
60+
# class TreeNode:
61+
# def __init__(self, x):
62+
# self.val = x
63+
# self.left = None
64+
# self.right = None
65+
66+
class Solution:
67+
def inorderSuccessor(self, root: 'TreeNode', p: 'TreeNode') -> 'TreeNode':
68+
cur, ans = root, None
69+
while cur:
70+
if cur.val <= p.val:
71+
cur = cur.right
72+
else:
73+
ans = cur
74+
cur = cur.left
75+
return ans
5876
```
5977

6078
### **Java**
6179

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

6482
```java
83+
/**
84+
* Definition for a binary tree node.
85+
* public class TreeNode {
86+
* int val;
87+
* TreeNode left;
88+
* TreeNode right;
89+
* TreeNode(int x) { val = x; }
90+
* }
91+
*/
92+
class Solution {
93+
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
94+
TreeNode cur = root, ans = null;
95+
while (cur != null) {
96+
if (cur.val <= p.val) {
97+
cur = cur.right;
98+
} else {
99+
ans = cur;
100+
cur = cur.left;
101+
}
102+
}
103+
return ans;
104+
}
105+
}
106+
```
107+
108+
### **Go**
65109

110+
```go
111+
/**
112+
* Definition for a binary tree node.
113+
* type TreeNode struct {
114+
* Val int
115+
* Left *TreeNode
116+
* Right *TreeNode
117+
* }
118+
*/
119+
func inorderSuccessor(root *TreeNode, p *TreeNode) (ans *TreeNode) {
120+
cur := root
121+
for cur != nil {
122+
if cur.Val <= p.Val {
123+
cur = cur.Right
124+
} else {
125+
ans = cur
126+
cur = cur.Left
127+
}
128+
}
129+
return
130+
}
66131
```
132+
133+
### **C++**
134+
135+
```cpp
136+
/**
137+
* Definition for a binary tree node.
138+
* struct TreeNode {
139+
* int val;
140+
* TreeNode *left;
141+
* TreeNode *right;
142+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
143+
* };
144+
*/
145+
class Solution {
146+
public:
147+
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
148+
TreeNode *cur = root, *ans = nullptr;
149+
while (cur != nullptr) {
150+
if (cur->val <= p->val) {
151+
cur = cur->right;
152+
} else {
153+
ans = cur;
154+
cur = cur->left;
155+
}
156+
}
157+
return ans;
158+
}
159+
};
160+
```
161+
67162
### **JavaScript**
68163
69164
```js
@@ -110,7 +205,6 @@ var inorderSuccessor = function (root, p) {
110205
};
111206
```
112207

113-
114208
### **...**
115209

116210
```

0 commit comments

Comments
 (0)