Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc/lcci problems #2648

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 71 additions & 131 deletions lcci/04.06.Successor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,18 @@

## 解法

### 方法一
### 方法一:二分搜索

二叉搜索树的中序遍历是一个升序序列,因此可以使用二分搜索的方法。

二叉搜索树节点 $p$ 的中序后继节点满足:

1. 中序后继的节点值大于 $p$ 的节点值
2. 中序后继是所有大于 $p$ 的节点中值最小的节点

因此,对于当前节点 $root$,如果 $root.val \gt p.val$,则 $root$ 可能是 $p$ 的中序后继节点,将 $root$ 记为 $ans$,然后搜索左子树,即 $root = root.left$;如果 $root.val \leq p.val$,则 $root$ 不能是 $p$ 的中序后继节点,搜索右子树,即 $root = root.right$。

时间复杂度 $O(h)$,其中 $h$ 为二叉搜索树的高度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -49,19 +60,14 @@


class Solution:
def inorderSuccessor(self, root: TreeNode, p: TreeNode) -> TreeNode:
def dfs(root):
if root is None:
return
dfs(root.left)
nonlocal ans, prev
if prev == p:
def inorderSuccessor(self, root: TreeNode, p: TreeNode) -> Optional[TreeNode]:
ans = None
while root:
if root.val > p.val:
ans = root
prev = root
dfs(root.right)

ans = prev = None
dfs(root)
root = root.left
else:
root = root.right
return ans
```

Expand All @@ -76,28 +82,17 @@ class Solution:
* }
*/
class Solution {
private TreeNode prev;
private TreeNode p;
private TreeNode ans;

public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
prev = null;
ans = null;
this.p = p;
dfs(root);
return ans;
}

private void dfs(TreeNode root) {
if (root == null) {
return;
}
dfs(root.left);
if (prev == p) {
ans = root;
TreeNode ans = null;
while (root != null) {
if (root.val > p.val) {
ans = root;
root = root.left;
} else {
root = root.right;
}
}
prev = root;
dfs(root.right);
return ans;
}
}
```
Expand All @@ -114,23 +109,18 @@ class Solution {
*/
class Solution {
public:
TreeNode* prev;
TreeNode* p;
TreeNode* ans;

TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
this->p = p;
dfs(root);
TreeNode* ans = nullptr;
while (root) {
if (root->val > p->val) {
ans = root;
root = root->left;
} else {
root = root->right;
}
}
return ans;
}

void dfs(TreeNode* root) {
if (!root) return;
dfs(root->left);
if (prev == p) ans = root;
prev = root;
dfs(root->right);
}
};
```

Expand All @@ -143,91 +133,46 @@ public:
* Right *TreeNode
* }
*/
func inorderSuccessor(root *TreeNode, p *TreeNode) *TreeNode {
var prev, ans *TreeNode
var dfs func(root *TreeNode)
dfs = func(root *TreeNode) {
if root == nil {
return
}
dfs(root.Left)
if prev == p {
func inorderSuccessor(root *TreeNode, p *TreeNode) (ans *TreeNode) {
for root != nil {
if root.Val > p.Val {
ans = root
root = root.Left
} else {
root = root.Right
}
prev = root
dfs(root.Right)
}
dfs(root)
return ans
return
}
```

```js
```ts
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} p
* @return {TreeNode}
*/
var inorderSuccessor = function (root, p) {
if (root == null) {
return root;
}
const { val, left, right } = root;
const res = inorderSuccessor(left, p);
if (res != null) {
return res;
}
if (val > p.val) {
return root;
}
return inorderSuccessor(right, p);
};
```

<!-- tabs:end -->

### 方法二

<!-- tabs:start -->

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
stack<TreeNode*> stk;
TreeNode* cur = root;
while (cur != nullptr || !stk.empty()) {
if (cur == nullptr) {
cur = stk.top();
stk.pop();
if (cur->val > p->val) {
return cur;
}
cur = cur->right;
} else {
stk.push(cur);
cur = cur->left;
}
function inorderSuccessor(root: TreeNode | null, p: TreeNode | null): TreeNode | null {
let ans: TreeNode | null = null;
while (root) {
if (root.val > p.val) {
ans = root;
root = root.left;
} else {
root = root.right;
}
return cur;
}
};
return ans;
}
```

```js
Expand All @@ -244,21 +189,16 @@ public:
* @return {TreeNode}
*/
var inorderSuccessor = function (root, p) {
const stack = [];
let cur = root;
while (cur != null || stack.length !== 0) {
if (cur == null) {
cur = stack.pop();
if (cur.val > p.val) {
return cur;
}
cur = cur.right;
let ans = null;
while (root) {
if (root.val > p.val) {
ans = root;
root = root.left;
} else {
stack.push(cur);
cur = cur.left;
root = root.right;
}
}
return cur;
return ans;
};
```

Expand Down
Loading
Loading