|
12 | 12 |
|
13 | 13 | <p><strong>示例 1:</strong></p>
|
14 | 14 |
|
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> |
16 | 16 |
|
17 | 17 | <pre>
|
18 | 18 | <strong>输入:</strong>root = [2,1,3], p = 1
|
|
22 | 22 |
|
23 | 23 | <p><strong>示例 2:</strong></p>
|
24 | 24 |
|
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> |
26 | 26 |
|
27 | 27 | <pre>
|
28 | 28 | <strong>输入:</strong>root = [5,3,6,2,4,null,null,1], p = 6
|
|
49 | 49 |
|
50 | 50 | <!-- 这里可写通用的实现逻辑 -->
|
51 | 51 |
|
| 52 | +利用二叉搜索树的特性,`p` 的中序后继一定是所有大于 `p` 的节点中最小的那个 |
| 53 | + |
52 | 54 | <!-- tabs:start -->
|
53 | 55 |
|
54 | 56 | ### **Python3**
|
55 | 57 |
|
56 | 58 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
57 | 59 |
|
58 | 60 | ```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 |
60 | 78 | ```
|
61 | 79 |
|
62 | 80 | ### **Java**
|
63 | 81 |
|
64 | 82 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
65 | 83 |
|
66 | 84 | ```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 | +``` |
67 | 134 |
|
| 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 | +}; |
68 | 162 | ```
|
69 | 163 |
|
70 | 164 | ### **...**
|
|
0 commit comments