Skip to content

Commit bb473bc

Browse files
Day 11-q3 solution added #319
1 parent 4028c16 commit bb473bc

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Approach:
2+
<br/> Using Stack:
3+
```
4+
class Solution {
5+
public:
6+
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
7+
vector<TreeNode*> stk;
8+
for (int i = 0; i < nums.size(); ++i)
9+
{
10+
TreeNode* cur = new TreeNode(nums[i]);
11+
while (!stk.empty() && stk.back()->val < nums[i])
12+
{
13+
cur->left = stk.back();
14+
stk.pop_back();
15+
}
16+
if (!stk.empty())
17+
stk.back()->right = cur;
18+
stk.push_back(cur);
19+
}
20+
return stk.front();
21+
}
22+
};
23+
```

0 commit comments

Comments
 (0)