Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0776
Browse files Browse the repository at this point in the history
No.0776.Split BST
  • Loading branch information
yanglbme committed Sep 10, 2022
1 parent 9bec4c9 commit 930dae6
Show file tree
Hide file tree
Showing 11 changed files with 676 additions and 5 deletions.
78 changes: 78 additions & 0 deletions solution/0600-0699/0669.Trim a Binary Search Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,84 @@ func trimBST(root *TreeNode, low int, high int) *TreeNode {
}
```

### **JavaScript**

```js
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} low
* @param {number} high
* @return {TreeNode}
*/
var trimBST = function (root, low, high) {
function dfs(root) {
if (!root) {
return root;
}
if (root.val < low) {
return dfs(root.right);
}
if (root.val > high) {
return dfs(root.left);
}
root.left = dfs(root.left);
root.right = dfs(root.right);
return root;
}
return dfs(root);
};
```

```js
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} low
* @param {number} high
* @return {TreeNode}
*/
var trimBST = function (root, low, high) {
while (root && (root.val < low || root.val > high)) {
root = root.val < low ? root.right : root.left;
}
if (!root) {
return root;
}
let node = root;
while (node.left) {
if (node.left.val < low) {
node.left = node.left.right;
} else {
node = node.left;
}
}
node = root;
while (node.right) {
if (node.right.val > high) {
node.right = node.right.left;
} else {
node = node.right;
}
}
return root;
};
```

### **...**

```
Expand Down
78 changes: 78 additions & 0 deletions solution/0600-0699/0669.Trim a Binary Search Tree/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,84 @@ func trimBST(root *TreeNode, low int, high int) *TreeNode {
}
```

### **JavaScript**

```js
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} low
* @param {number} high
* @return {TreeNode}
*/
var trimBST = function (root, low, high) {
function dfs(root) {
if (!root) {
return root;
}
if (root.val < low) {
return dfs(root.right);
}
if (root.val > high) {
return dfs(root.left);
}
root.left = dfs(root.left);
root.right = dfs(root.right);
return root;
}
return dfs(root);
};
```

```js
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} low
* @param {number} high
* @return {TreeNode}
*/
var trimBST = function (root, low, high) {
while (root && (root.val < low || root.val > high)) {
root = root.val < low ? root.right : root.left;
}
if (!root) {
return root;
}
let node = root;
while (node.left) {
if (node.left.val < low) {
node.left = node.left.right;
} else {
node = node.left;
}
}
node = root;
while (node.right) {
if (node.right.val > high) {
node.right = node.right.left;
} else {
node = node.right;
}
}
return root;
};
```

### **...**

```
Expand Down
31 changes: 31 additions & 0 deletions solution/0600-0699/0669.Trim a Binary Search Tree/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} low
* @param {number} high
* @return {TreeNode}
*/
var trimBST = function (root, low, high) {
function dfs(root) {
if (!root) {
return root;
}
if (root.val < low) {
return dfs(root.right);
}
if (root.val > high) {
return dfs(root.left);
}
root.left = dfs(root.left);
root.right = dfs(root.right);
return root;
}
return dfs(root);
};
174 changes: 173 additions & 1 deletion solution/0700-0799/0776.Split BST/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,194 @@

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

**方法一:递归**

判断 `root` 节点的情况:

-`root` 为空,直接返回 `[null, null]`
-`root.val <= target`,说明 `root` 及其左孩子所有节点的值均小于等于 `target`,那么我们递归 `root.right`,得到 `ans`。然后将 `root.right` 指向 `ans[0]`,最后返回 `[root, ans[1]]`
-`root.val > target`,说明 `root` 及其右孩子所有节点的值均大于 `target`,那么我们递归 `root.left`,得到 `ans`。然后将 `root.left` 指向 `ans[1]`,最后返回 `[ans[0], root]`

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉搜索树的节点个数。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def splitBST(self, root: Optional[TreeNode], target: int) -> List[Optional[TreeNode]]:
def dfs(root):
if root is None:
return [None, None]
if root.val <= target:
l, r = dfs(root.right)
root.right = l
return [root, r]
else:
l, r = dfs(root.left)
root.left = r
return [l, root]

return dfs(root)
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private int t;

public TreeNode[] splitBST(TreeNode root, int target) {
t = target;
return dfs(root);
}

private TreeNode[] dfs(TreeNode root) {
if (root == null) {
return new TreeNode[]{null, null};
}
if (root.val <= t) {
TreeNode[] ans = dfs(root.right);
root.right = ans[0];
ans[0] = root;
return ans;
} else {
TreeNode[] ans = dfs(root.left);
root.left = ans[1];
ans[1] = root;
return ans;
}
}
}
```

### **C++**

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int t;

vector<TreeNode*> splitBST(TreeNode* root, int target) {
t = target;
return dfs(root);
}

vector<TreeNode*> dfs(TreeNode* root) {
if (!root) return {nullptr, nullptr};
if (root->val <= t) {
auto ans = dfs(root->right);
root->right = ans[0];
ans[0] = root;
return ans;
} else {
auto ans = dfs(root->left);
root->left = ans[1];
ans[1] = root;
return ans;
}
}
};
```
### **Go**
```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func splitBST(root *TreeNode, target int) []*TreeNode {
if root == nil {
return []*TreeNode{nil, nil}
}
if root.Val <= target {
ans := splitBST(root.Right, target)
root.Right = ans[0]
ans[0] = root
return ans
} else {
ans := splitBST(root.Left, target)
root.Left = ans[1]
ans[1] = root
return ans
}
}
```

### **JavaScript**

```js
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} target
* @return {TreeNode[]}
*/
var splitBST = function (root, target) {
let ans = [null, null];
if (!root) {
return ans;
}
if (root.val <= target) {
ans = splitBST(root.right, target);
root.right = ans[0];
ans[0] = root;
} else {
ans = splitBST(root.left, target);
root.left = ans[1];
ans[1] = root;
}
return ans;
};
```

### **...**
Expand Down
Loading

0 comments on commit 930dae6

Please sign in to comment.