Skip to content

Commit 15d8d08

Browse files
authored
feat: add solutions to lc/lcof2 problems (#1404)
* lc No.0285.Inorder Successor in BST * lcof2 No.053.二叉搜索树中的中序后继
1 parent 9a2661b commit 15d8d08

File tree

14 files changed

+456
-282
lines changed

14 files changed

+456
-282
lines changed

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

+85-41
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,18 @@
4848

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

51-
利用二叉搜索树的特性,`p` 的中序后继一定是所有大于 `p` 的节点中最小的那个
51+
**方法一:二分搜索**
52+
53+
二叉搜索树的中序遍历是一个升序序列,因此可以使用二分搜索的方法。
54+
55+
二叉搜索树节点 $p$ 的中序后继节点满足:
56+
57+
1. 中序后继的节点值大于 $p$ 的节点值
58+
2. 中序后继是所有大于 $p$ 的节点中值最小的节点
59+
60+
因此,对于当前节点 $root$,如果 $root.val \gt p.val$,则 $root$ 可能是 $p$ 的中序后继节点,将 $root$ 记为 $ans$,然后搜索左子树,即 $root = root.left$;如果 $root.val \leq p.val$,则 $root$ 不能是 $p$ 的中序后继节点,搜索右子树,即 $root = root.right$。
61+
62+
时间复杂度 $O(h)$,其中 $h$ 为二叉搜索树的高度。空间复杂度 $O(1)$。
5263

5364
<!-- tabs:start -->
5465

@@ -66,14 +77,14 @@
6677

6778

6879
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
80+
def inorderSuccessor(self, root: "TreeNode", p: "TreeNode") -> "TreeNode":
81+
ans = None
82+
while root:
83+
if root.val > p.val:
84+
ans = root
85+
root = root.left
7486
else:
75-
ans = cur
76-
cur = cur.left
87+
root = root.right
7788
return ans
7889
```
7990

@@ -93,20 +104,49 @@ class Solution:
93104
*/
94105
class Solution {
95106
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;
107+
TreeNode ans = null;
108+
while (root != null) {
109+
if (root.val > p.val) {
110+
ans = root;
111+
root = root.left;
100112
} else {
101-
ans = cur;
102-
cur = cur.left;
113+
root = root.right;
103114
}
104115
}
105116
return ans;
106117
}
107118
}
108119
```
109120

121+
### **C++**
122+
123+
```cpp
124+
/**
125+
* Definition for a binary tree node.
126+
* struct TreeNode {
127+
* int val;
128+
* TreeNode *left;
129+
* TreeNode *right;
130+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
131+
* };
132+
*/
133+
class Solution {
134+
public:
135+
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
136+
TreeNode* ans = nullptr;
137+
while (root) {
138+
if (root->val > p->val) {
139+
ans = root;
140+
root = root->left;
141+
} else {
142+
root = root->right;
143+
}
144+
}
145+
return ans;
146+
}
147+
};
148+
```
149+
110150
### **Go**
111151
112152
```go
@@ -119,46 +159,50 @@ class Solution {
119159
* }
120160
*/
121161
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
162+
for root != nil {
163+
if root.Val > p.Val {
164+
ans = root
165+
root = root.Left
126166
} else {
127-
ans = cur
128-
cur = cur.Left
167+
root = root.Right
129168
}
130169
}
131170
return
132171
}
133172
```
134173

135-
### **C++**
174+
### **TypeScript**
136175

137-
```cpp
176+
```ts
138177
/**
139178
* 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-
* };
179+
* class TreeNode {
180+
* val: number
181+
* left: TreeNode | null
182+
* right: TreeNode | null
183+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
184+
* this.val = (val===undefined ? 0 : val)
185+
* this.left = (left===undefined ? null : left)
186+
* this.right = (right===undefined ? null : right)
187+
* }
188+
* }
146189
*/
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-
}
190+
191+
function inorderSuccessor(
192+
root: TreeNode | null,
193+
p: TreeNode | null,
194+
): TreeNode | null {
195+
let ans: TreeNode | null = null;
196+
while (root) {
197+
if (root.val > p.val) {
198+
ans = root;
199+
root = root.left;
200+
} else {
201+
root = root.right;
158202
}
159-
return ans;
160203
}
161-
};
204+
return ans;
205+
}
162206
```
163207

164208
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,24 +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-
};
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* ans = nullptr;
14+
while (root) {
15+
if (root->val > p->val) {
16+
ans = root;
17+
root = root->left;
18+
} else {
19+
root = root->right;
20+
}
21+
}
22+
return ans;
23+
}
24+
};

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
* }
88
*/
99
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
10+
for root != nil {
11+
if root.Val > p.Val {
12+
ans = root
13+
root = root.Left
1414
} else {
15-
ans = cur
16-
cur = cur.Left
15+
root = root.Right
1716
}
1817
}
1918
return
Original file line numberDiff line numberDiff line change
@@ -1,23 +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-
}
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 ans = null;
13+
while (root != null) {
14+
if (root.val > p.val) {
15+
ans = root;
16+
root = root.left;
17+
} else {
18+
root = root.right;
19+
}
20+
}
21+
return ans;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
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-
9-
class Solution:
10-
def inorderSuccessor(self, root: 'TreeNode', p: 'TreeNode') -> 'TreeNode':
11-
cur, ans = root, None
12-
while cur:
13-
if cur.val <= p.val:
14-
cur = cur.right
15-
else:
16-
ans = cur
17-
cur = cur.left
18-
return ans
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+
9+
class Solution:
10+
def inorderSuccessor(self, root: "TreeNode", p: "TreeNode") -> "TreeNode":
11+
ans = None
12+
while root:
13+
if root.val > p.val:
14+
ans = root
15+
root = root.left
16+
else:
17+
root = root.right
18+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function inorderSuccessor(
16+
root: TreeNode | null,
17+
p: TreeNode | null,
18+
): TreeNode | null {
19+
let ans: TreeNode | null = null;
20+
while (root) {
21+
if (root.val > p.val) {
22+
ans = root;
23+
root = root.left;
24+
} else {
25+
root = root.right;
26+
}
27+
}
28+
return ans;
29+
}

0 commit comments

Comments
 (0)