35
35
36
36
## 解法
37
37
38
- ### 方法一
38
+ ### 方法一:二分搜索
39
+
40
+ 二叉搜索树的中序遍历是一个升序序列,因此可以使用二分搜索的方法。
41
+
42
+ 二叉搜索树节点 $p$ 的中序后继节点满足:
43
+
44
+ 1 . 中序后继的节点值大于 $p$ 的节点值
45
+ 2 . 中序后继是所有大于 $p$ 的节点中值最小的节点
46
+
47
+ 因此,对于当前节点 $root$,如果 $root.val \gt p.val$,则 $root$ 可能是 $p$ 的中序后继节点,将 $root$ 记为 $ans$,然后搜索左子树,即 $root = root.left$;如果 $root.val \leq p.val$,则 $root$ 不能是 $p$ 的中序后继节点,搜索右子树,即 $root = root.right$。
48
+
49
+ 时间复杂度 $O(h)$,其中 $h$ 为二叉搜索树的高度。空间复杂度 $O(1)$。
39
50
40
51
<!-- tabs:start -->
41
52
49
60
50
61
51
62
class Solution :
52
- def inorderSuccessor (self , root : TreeNode, p : TreeNode) -> TreeNode:
53
- def dfs (root ):
54
- if root is None :
55
- return
56
- dfs(root.left)
57
- nonlocal ans, prev
58
- if prev == p:
63
+ def inorderSuccessor (self , root : TreeNode, p : TreeNode) -> Optional[TreeNode]:
64
+ ans = None
65
+ while root:
66
+ if root.val > p.val:
59
67
ans = root
60
- prev = root
61
- dfs(root.right)
62
-
63
- ans = prev = None
64
- dfs(root)
68
+ root = root.left
69
+ else :
70
+ root = root.right
65
71
return ans
66
72
```
67
73
@@ -76,28 +82,17 @@ class Solution:
76
82
* }
77
83
*/
78
84
class Solution {
79
- private TreeNode prev;
80
- private TreeNode p;
81
- private TreeNode ans;
82
-
83
85
public TreeNode inorderSuccessor (TreeNode root , TreeNode p ) {
84
- prev = null ;
85
- ans = null ;
86
- this . p = p;
87
- dfs(root);
88
- return ans;
89
- }
90
-
91
- private void dfs (TreeNode root ) {
92
- if (root == null ) {
93
- return ;
94
- }
95
- dfs(root. left);
96
- if (prev == p) {
97
- ans = root;
86
+ TreeNode ans = null ;
87
+ while (root != null ) {
88
+ if (root. val > p. val) {
89
+ ans = root;
90
+ root = root. left;
91
+ } else {
92
+ root = root. right;
93
+ }
98
94
}
99
- prev = root;
100
- dfs(root. right);
95
+ return ans;
101
96
}
102
97
}
103
98
```
@@ -114,23 +109,18 @@ class Solution {
114
109
*/
115
110
class Solution {
116
111
public:
117
- TreeNode* prev;
118
- TreeNode* p;
119
- TreeNode* ans;
120
-
121
112
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
122
- this->p = p;
123
- dfs(root);
113
+ TreeNode* ans = nullptr;
114
+ while (root) {
115
+ if (root->val > p->val) {
116
+ ans = root;
117
+ root = root->left;
118
+ } else {
119
+ root = root->right;
120
+ }
121
+ }
124
122
return ans;
125
123
}
126
-
127
- void dfs (TreeNode* root) {
128
- if (!root) return;
129
- dfs(root->left);
130
- if (prev == p) ans = root;
131
- prev = root;
132
- dfs(root->right);
133
- }
134
124
};
135
125
```
136
126
@@ -143,91 +133,46 @@ public:
143
133
* Right *TreeNode
144
134
* }
145
135
*/
146
- func inorderSuccessor(root *TreeNode, p *TreeNode) *TreeNode {
147
- var prev, ans *TreeNode
148
- var dfs func(root *TreeNode)
149
- dfs = func(root *TreeNode) {
150
- if root == nil {
151
- return
152
- }
153
- dfs(root.Left)
154
- if prev == p {
136
+ func inorderSuccessor(root *TreeNode, p *TreeNode) (ans *TreeNode) {
137
+ for root != nil {
138
+ if root.Val > p.Val {
155
139
ans = root
140
+ root = root.Left
141
+ } else {
142
+ root = root.Right
156
143
}
157
- prev = root
158
- dfs(root.Right)
159
144
}
160
- dfs(root)
161
- return ans
145
+ return
162
146
}
163
147
```
164
148
165
- ``` js
149
+ ``` ts
166
150
/**
167
151
* Definition for a binary tree node.
168
- * function TreeNode(val) {
169
- * this.val = val;
170
- * this.left = this.right = null;
152
+ * class TreeNode {
153
+ * val: number
154
+ * left: TreeNode | null
155
+ * right: TreeNode | null
156
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
157
+ * this.val = (val===undefined ? 0 : val)
158
+ * this.left = (left===undefined ? null : left)
159
+ * this.right = (right===undefined ? null : right)
160
+ * }
171
161
* }
172
162
*/
173
- /**
174
- * @param {TreeNode} root
175
- * @param {TreeNode} p
176
- * @return {TreeNode}
177
- */
178
- var inorderSuccessor = function (root , p ) {
179
- if (root == null ) {
180
- return root;
181
- }
182
- const { val , left , right } = root;
183
- const res = inorderSuccessor (left, p);
184
- if (res != null ) {
185
- return res;
186
- }
187
- if (val > p .val ) {
188
- return root;
189
- }
190
- return inorderSuccessor (right, p);
191
- };
192
- ```
193
-
194
- <!-- tabs:end -->
195
-
196
- ### 方法二
197
163
198
- <!-- tabs:start -->
199
-
200
- ``` cpp
201
- /* *
202
- * Definition for a binary tree node.
203
- * struct TreeNode {
204
- * int val;
205
- * TreeNode *left;
206
- * TreeNode *right;
207
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
208
- * };
209
- */
210
- class Solution {
211
- public:
212
- TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
213
- stack<TreeNode* > stk;
214
- TreeNode* cur = root;
215
- while (cur != nullptr || !stk.empty()) {
216
- if (cur == nullptr) {
217
- cur = stk.top();
218
- stk.pop();
219
- if (cur->val > p->val) {
220
- return cur;
221
- }
222
- cur = cur->right;
223
- } else {
224
- stk.push(cur);
225
- cur = cur->left;
226
- }
164
+ function inorderSuccessor(root : TreeNode | null , p : TreeNode | null ): TreeNode | null {
165
+ let ans: TreeNode | null = null ;
166
+ while (root ) {
167
+ if (root .val > p .val ) {
168
+ ans = root ;
169
+ root = root .left ;
170
+ } else {
171
+ root = root .right ;
227
172
}
228
- return cur;
229
173
}
230
- };
174
+ return ans ;
175
+ }
231
176
```
232
177
233
178
``` js
@@ -244,21 +189,16 @@ public:
244
189
* @return {TreeNode}
245
190
*/
246
191
var inorderSuccessor = function (root , p ) {
247
- const stack = [];
248
- let cur = root;
249
- while (cur != null || stack.length !== 0) {
250
- if (cur == null) {
251
- cur = stack.pop();
252
- if (cur.val > p.val) {
253
- return cur;
254
- }
255
- cur = cur.right;
192
+ let ans = null ;
193
+ while (root) {
194
+ if (root .val > p .val ) {
195
+ ans = root;
196
+ root = root .left ;
256
197
} else {
257
- stack.push(cur);
258
- cur = cur.left;
198
+ root = root .right ;
259
199
}
260
200
}
261
- return cur ;
201
+ return ans ;
262
202
};
263
203
```
264
204
0 commit comments