Skip to content

Commit c6a9ccc

Browse files
add 1382
1 parent e12d265 commit c6a9ccc

File tree

7 files changed

+127
-4
lines changed

7 files changed

+127
-4
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -672,4 +672,5 @@ LeetCode
672672
|1376|[Time Needed to Inform All Employees](https://leetcode.com/problems/time-needed-to-inform-all-employees/)|c|[c++](./src/1376-Time-Needed-to-Inform-All-Employees/1376.cpp)|[python](./src/1376-Time-Needed-to-Inform-All-Employees/1376.py)|[go](./src/1376-Time-Needed-to-Inform-All-Employees/1376.go)|[js](./src/1376-Time-Needed-to-Inform-All-Employees/1376.js)|[java](./src/1376-Time-Needed-to-Inform-All-Employees/1376.java)|Medium|
673673
|1377|[Frog Position After T Seconds](https://leetcode.com/problems/frog-position-after-t-seconds/)|c|[c++](./src/1377-Frog-Position-After-T-Seconds/1377.cpp)|[python](./src/1377-Frog-Position-After-T-Seconds/1377.py)|[go](./src/1377-Frog-Position-After-T-Seconds/1377.go)|[js](./src/1377-Frog-Position-After-T-Seconds/1377.js)|[java](./src/1377-Frog-Position-After-T-Seconds/1377.java)|Hard|
674674
|1380|[Lucky Numbers in a Matrix](https://leetcode.com/problems/lucky-numbers-in-a-matrix/)|c|[c++](./src/1380-Lucky-Numbers-in-a-Matrix/1380.cpp)|[python](./src/1380-Lucky-Numbers-in-a-Matrix/1380.py)|[go](./src/1380-Lucky-Numbers-in-a-Matrix/1380.go)|[js](./src/1380-Lucky-Numbers-in-a-Matrix/1380.js)|[java](./src/1380-Lucky-Numbers-in-a-Matrix/1380.java)|Easy|
675-
|1381|[Design a Stack With Increment Operation](https://leetcode.com/problems/design-a-stack-with-increment-operation/)|c|[c++](./src/1381-Design-a-Stack-With-Increment-Operation/1381.cpp)|[python](./src/1381-Design-a-Stack-With-Increment-Operation/1381.py)|[go](./src/1381-Design-a-Stack-With-Increment-Operation/1381.go)|[js](./src/1381-Design-a-Stack-With-Increment-Operation/1381.js)|[java](./src/1381-Design-a-Stack-With-Increment-Operation/1381.java)|Medium|
675+
|1381|[Design a Stack With Increment Operation](https://leetcode.com/problems/design-a-stack-with-increment-operation/)|c|[c++](./src/1381-Design-a-Stack-With-Increment-Operation/1381.cpp)|[python](./src/1381-Design-a-Stack-With-Increment-Operation/1381.py)|[go](./src/1381-Design-a-Stack-With-Increment-Operation/1381.go)|[js](./src/1381-Design-a-Stack-With-Increment-Operation/1381.js)|[java](./src/1381-Design-a-Stack-With-Increment-Operation/1381.java)|Medium|
676+
|1382|[Balance a Binary Search Tree](https://leetcode.com/problems/balance-a-binary-search-tree/)|c|[c++](./src/1382-Balance-a-Binary-Search-Tree/1382.cpp)|[python](./src/1382-Balance-a-Binary-Search-Tree/1382.py)|[go](./src/1382-Balance-a-Binary-Search-Tree/1382.go)|[js](./src/1382-Balance-a-Binary-Search-Tree/1382.js)|[java](./src/1382-Balance-a-Binary-Search-Tree/1382.java)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
TreeNode* balanceBST(TreeNode* root) {
4+
inOrder(root);
5+
return sortedToBST(0, res.size() - 1);
6+
}
7+
8+
private:
9+
vector<int> res;
10+
11+
void inOrder(TreeNode* root) {
12+
if (root != nullptr) {
13+
inOrder(root->left);
14+
res.emplace_back(root->val);
15+
inOrder(root->right);
16+
}
17+
}
18+
19+
TreeNode* sortedToBST(int l, int r) {
20+
if (l > r) return nullptr;
21+
int mid = (l + r) >> 1;
22+
TreeNode* root = new TreeNode(res[mid]);
23+
root->left = sortedToBST(l, mid - 1);
24+
root->right = sortedToBST(mid + 1, r);
25+
return root;
26+
}
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var res []int
2+
3+
func balanceBST(root *TreeNode) *TreeNode {
4+
res = []int{}
5+
inOrder(root)
6+
return sortedToBST(0, len(res) - 1)
7+
}
8+
9+
func inOrder(root *TreeNode) {
10+
if root != nil {
11+
inOrder(root.Left)
12+
res = append(res, root.Val)
13+
inOrder(root.Right)
14+
}
15+
}
16+
17+
func sortedToBST(l, r int) *TreeNode {
18+
if l > r {
19+
return nil
20+
}
21+
22+
mid := (l + r) >> 1
23+
root := new(TreeNode)
24+
root.Val = res[mid]
25+
root.Left = sortedToBST(l, mid - 1)
26+
root.Right = sortedToBST(mid + 1, r)
27+
return root
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public TreeNode balanceBST(TreeNode root) {
3+
inOrder(root);
4+
return sortedToBST(0, res.size() - 1);
5+
}
6+
7+
private List<Integer> res = new ArrayList();
8+
9+
private void inOrder(TreeNode root) {
10+
if (root != null) {
11+
inOrder(root.left);
12+
res.add(root.val);
13+
inOrder(root.right);
14+
}
15+
}
16+
17+
private TreeNode sortedToBST(int l, int r) {
18+
if (l > r) return null;
19+
int mid = (l + r) >> 1;
20+
TreeNode root = new TreeNode(res.get(mid));
21+
root.left = sortedToBST(l, mid - 1);
22+
root.right = sortedToBST(mid + 1, r);
23+
return root;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var balanceBST = function(root) {
2+
let res = [];
3+
let inOrder = function(root) {
4+
if (root != null) {
5+
inOrder(root.left);
6+
res.push(root.val);
7+
inOrder(root.right);
8+
}
9+
}
10+
11+
inOrder(root);
12+
13+
let sortedToBST = function(l, r) {
14+
if (l > r) return null;
15+
let mid = (l + r) >> 1;
16+
let root = new TreeNode(res[mid]);
17+
root.left = sortedToBST(l, mid - 1);
18+
root.right = sortedToBST(mid + 1, r);
19+
return root;
20+
}
21+
return sortedToBST(0, res.length - 1);
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def balanceBST(self, root: TreeNode) -> TreeNode:
3+
res = []
4+
def inOrder(node):
5+
if node:
6+
inOrder(node.left)
7+
res.append(node.val)
8+
inOrder(node.right)
9+
10+
inOrder(root)
11+
12+
def sortedToBST(l, r):
13+
if l > r: return None
14+
mid = (l + r) >> 1
15+
root = TreeNode(res[mid])
16+
root.left = sortedToBST(l, mid - 1)
17+
root.right = sortedToBST(mid + 1, r)
18+
return root
19+
20+
return sortedToBST(0, len(res)-1)

src/addProb.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import os, bisect
33

44
# 题目名称
5-
name = "Design a Stack With Increment Operation"
6-
ID = 1381
7-
url = "https://leetcode.com/problems/design-a-stack-with-increment-operation/"
5+
name = "Balance a Binary Search Tree"
6+
ID = 1382
7+
url = "https://leetcode.com/problems/balance-a-binary-search-tree/"
88
difficult = "Medium"
99
prog = ['c', 'cpp', 'py', 'go', 'js', 'java']
1010

0 commit comments

Comments
 (0)