48
48
49
49
<!-- 这里可写通用的实现逻辑 -->
50
50
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)$。
52
63
53
64
<!-- tabs:start -->
54
65
66
77
67
78
68
79
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
74
86
else :
75
- ans = cur
76
- cur = cur.left
87
+ root = root.right
77
88
return ans
78
89
```
79
90
@@ -93,20 +104,49 @@ class Solution:
93
104
*/
94
105
class Solution {
95
106
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;
100
112
} else {
101
- ans = cur;
102
- cur = cur. left;
113
+ root = root. right;
103
114
}
104
115
}
105
116
return ans;
106
117
}
107
118
}
108
119
```
109
120
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
+
110
150
### **Go**
111
151
112
152
```go
@@ -119,46 +159,50 @@ class Solution {
119
159
* }
120
160
*/
121
161
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
126
166
} else {
127
- ans = cur
128
- cur = cur.Left
167
+ root = root.Right
129
168
}
130
169
}
131
170
return
132
171
}
133
172
```
134
173
135
- ### ** C++ **
174
+ ### ** TypeScript **
136
175
137
- ``` cpp
176
+ ``` ts
138
177
/**
139
178
* 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
+ * }
146
189
*/
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 ;
158
202
}
159
- return ans;
160
203
}
161
- };
204
+ return ans ;
205
+ }
162
206
```
163
207
164
208
### ** ...**
0 commit comments