Skip to content

Commit 57aa6ac

Browse files
add 654
1 parent fb97e07 commit 57aa6ac

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ LeetCode
254254
|0560|[Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) | c | [c++](./src/0560-Subarray-Sum-Equals-K/0560.cpp) |[python](./src/0560-Subarray-Sum-Equals-K/0560.py)|||Medium|
255255
|0637|[Average of Levels in Binary Tree](https://leetcode.com/problems/subarray-sum-equals-k/) | c | [c++](./src/0637-Average-of-Levels-in-Binary-Tree/0637.cpp) |[python](./src/0637-Average-of-Levels-in-Binary-Tree/0637.py)|||Easy|
256256
|0652|[Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/) | c | [c++](./src/0652-Find-Duplicate-Subtrees/0652.cpp) |[python](./src/0652-Find-Duplicate-Subtrees/0652.py)|||Medium|
257+
|0654|[Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/) | c | [c++](./src/0654-Maximum-Binary-Tree/0654.cpp) |[python](./src/0654-Maximum-Binary-Tree/0654.py)|||Medium|
257258
|0684|[Redundant Connection](https://leetcode.com/problems/redundant-connection/) | c | [c++](./src/0684-Redundant-Connection/0684.cpp) |[python](./src/0684-Redundant-Connection/0684.py)|||Medium|
258259
|0687|[Longest Univalue Path](https://leetcode.com/problems/redundant-connection/) | c | [c++](./src/0687-Longest-Univalue-Path/0687.cpp) |[python](./src/0687-Longest-Univalue-Path/0687.py)|||Easy|
259260
|0739|[Daily Temperatures](https://leetcode.com/problems/daily-temperatures/) | c | [c++](./src/0739-Daily-Temperatures/0739.cpp) |[python](./src/0739-Daily-Temperatures/0739.py)|||Medium|

Diff for: src/0654-Maximum-Binary-Tree/0654.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
2+
class Solution
3+
{
4+
public:
5+
TreeNode* constructMaximumBinaryTree(vector<int>& nums)
6+
{
7+
vector<TreeNode*> stack;
8+
for (int n : nums)
9+
{
10+
TreeNode* cur = new TreeNode(n);
11+
while (!stack.empty() and (stack.back())->val < n)
12+
{
13+
cur->left = stack.back(), stack.pop_back();
14+
}
15+
16+
if (!stack.empty())
17+
{
18+
TreeNode* tmp = stack.back();
19+
tmp->right = cur;
20+
}
21+
stack.push_back(cur);
22+
}
23+
return stack[0];
24+
}
25+
};

Diff for: src/0654-Maximum-Binary-Tree/0654.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode:
3+
stack = list()
4+
for n in nums:
5+
cur = TreeNode(n)
6+
while stack and stack[-1].val < n:
7+
cur.left = stack.pop()
8+
9+
if stack:
10+
stack[-1].right = cur
11+
stack.append(cur)
12+
13+
return stack[0]

0 commit comments

Comments
 (0)