Skip to content

Commit d9e00c6

Browse files
committed
feat: add solutions to lc problem: No.0654
No.0654.Maximum Binary Tree
1 parent 36b3ef4 commit d9e00c6

File tree

2 files changed

+260
-0
lines changed

2 files changed

+260
-0
lines changed

Diff for: solution/0600-0699/0654.Maximum Binary Tree/README.md

+137
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@
6868

6969
最多需要查询 $n$ 次,因此,总的时间复杂度为 $O(nlogn)$,空间复杂度 $O(n)$,其中 $n$ 是数组的长度。
7070

71+
**方法三:单调栈**
72+
73+
题目表达了一个意思:如果 $nums$ 中间有一个数字 $v$,找出它左右两侧最大的数,这两个最大的数应该比 $v$ 小。
74+
75+
了解单调栈的朋友,或许会注意到:
76+
77+
当我们尝试向栈中压入一个数字 $v$ 时,如果栈顶元素比 $v$ 小,则循环弹出栈顶元素,并记录最后一个弹出的元素 $last$。那么循环结束,$last$ 必须位于 $v$ 的左侧,因为 $last$ 是 $v$ 的左侧最大的数。令 $node(val=v).left$ 指向 $last$。
78+
79+
如果此时存在栈顶元素,栈顶元素一定大于 $v$。$v$ 成为栈顶元素的候选右子树节点。令 $stk.top().right$ 指向 $v$。然后 $v$ 入栈。
80+
81+
遍历结束,栈底元素成为树的根节点。
82+
83+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
84+
7185
<!-- tabs:start -->
7286

7387
### **Python3**
@@ -158,6 +172,28 @@ class SegmentTree:
158172
self.tr[u].v = max(self.tr[u << 1].v, self.tr[u << 1 | 1].v)
159173
```
160174

175+
```python
176+
# Definition for a binary tree node.
177+
# class TreeNode:
178+
# def __init__(self, val=0, left=None, right=None):
179+
# self.val = val
180+
# self.left = left
181+
# self.right = right
182+
class Solution:
183+
def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:
184+
stk = []
185+
for v in nums:
186+
node = TreeNode(v)
187+
last = None
188+
while stk and stk[-1].val < v:
189+
last = stk.pop()
190+
node.left = last
191+
if stk:
192+
stk[-1].right = node
193+
stk.append(node)
194+
return stk[0]
195+
```
196+
161197
### **Java**
162198

163199
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -301,6 +337,42 @@ class SegmentTree {
301337
}
302338
```
303339

340+
```java
341+
/**
342+
* Definition for a binary tree node.
343+
* public class TreeNode {
344+
* int val;
345+
* TreeNode left;
346+
* TreeNode right;
347+
* TreeNode() {}
348+
* TreeNode(int val) { this.val = val; }
349+
* TreeNode(int val, TreeNode left, TreeNode right) {
350+
* this.val = val;
351+
* this.left = left;
352+
* this.right = right;
353+
* }
354+
* }
355+
*/
356+
class Solution {
357+
public TreeNode constructMaximumBinaryTree(int[] nums) {
358+
Deque<TreeNode> stk = new ArrayDeque<>();
359+
for (int v : nums) {
360+
TreeNode node = new TreeNode(v);
361+
TreeNode last = null;
362+
while (!stk.isEmpty() && stk.peek().val < v) {
363+
last = stk.pop();
364+
}
365+
node.left = last;
366+
if (!stk.isEmpty()) {
367+
stk.peek().right = node;
368+
}
369+
stk.push(node);
370+
}
371+
return stk.getLast();
372+
}
373+
}
374+
```
375+
304376
### **C++**
305377

306378
```cpp
@@ -422,6 +494,43 @@ public:
422494
};
423495
```
424496
497+
```cpp
498+
/**
499+
* Definition for a binary tree node.
500+
* struct TreeNode {
501+
* int val;
502+
* TreeNode *left;
503+
* TreeNode *right;
504+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
505+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
506+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
507+
* };
508+
*/
509+
class Solution {
510+
public:
511+
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
512+
stack<TreeNode*> stk;
513+
for (int v : nums) {
514+
TreeNode* node = new TreeNode(v);
515+
TreeNode* last = nullptr;
516+
while (!stk.empty() && stk.top()->val < v) {
517+
last = stk.top();
518+
stk.pop();
519+
}
520+
node->left = last;
521+
if (!stk.empty()) {
522+
stk.top()->right = node;
523+
}
524+
stk.push(node);
525+
}
526+
while (stk.size() > 1) {
527+
stk.pop();
528+
}
529+
return stk.top();
530+
}
531+
};
532+
```
533+
425534
### **Go**
426535

427536
```go
@@ -545,6 +654,34 @@ func max(a, b int) int {
545654
}
546655
```
547656

657+
```go
658+
/**
659+
* Definition for a binary tree node.
660+
* type TreeNode struct {
661+
* Val int
662+
* Left *TreeNode
663+
* Right *TreeNode
664+
* }
665+
*/
666+
func constructMaximumBinaryTree(nums []int) *TreeNode {
667+
stk := []*TreeNode{}
668+
for _, v := range nums {
669+
node := &TreeNode{Val: v}
670+
var last *TreeNode
671+
for len(stk) > 0 && stk[len(stk)-1].Val < v {
672+
last = stk[len(stk)-1]
673+
stk = stk[:len(stk)-1]
674+
}
675+
node.Left = last
676+
if len(stk) > 0 {
677+
stk[len(stk)-1].Right = node
678+
}
679+
stk = append(stk, node)
680+
}
681+
return stk[0]
682+
}
683+
```
684+
548685
### **C**
549686

550687
```c

Diff for: solution/0600-0699/0654.Maximum Binary Tree/README_EN.md

+123
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,28 @@ class SegmentTree:
138138
self.tr[u].v = max(self.tr[u << 1].v, self.tr[u << 1 | 1].v)
139139
```
140140

141+
```python
142+
# Definition for a binary tree node.
143+
# class TreeNode:
144+
# def __init__(self, val=0, left=None, right=None):
145+
# self.val = val
146+
# self.left = left
147+
# self.right = right
148+
class Solution:
149+
def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:
150+
stk = []
151+
for v in nums:
152+
node = TreeNode(v)
153+
last = None
154+
while stk and stk[-1].val < v:
155+
last = stk.pop()
156+
node.left = last
157+
if stk:
158+
stk[-1].right = node
159+
stk.append(node)
160+
return stk[0]
161+
```
162+
141163
### **Java**
142164

143165
```java
@@ -279,6 +301,42 @@ class SegmentTree {
279301
}
280302
```
281303

304+
```java
305+
/**
306+
* Definition for a binary tree node.
307+
* public class TreeNode {
308+
* int val;
309+
* TreeNode left;
310+
* TreeNode right;
311+
* TreeNode() {}
312+
* TreeNode(int val) { this.val = val; }
313+
* TreeNode(int val, TreeNode left, TreeNode right) {
314+
* this.val = val;
315+
* this.left = left;
316+
* this.right = right;
317+
* }
318+
* }
319+
*/
320+
class Solution {
321+
public TreeNode constructMaximumBinaryTree(int[] nums) {
322+
Deque<TreeNode> stk = new ArrayDeque<>();
323+
for (int v : nums) {
324+
TreeNode node = new TreeNode(v);
325+
TreeNode last = null;
326+
while (!stk.isEmpty() && stk.peek().val < v) {
327+
last = stk.pop();
328+
}
329+
node.left = last;
330+
if (!stk.isEmpty()) {
331+
stk.peek().right = node;
332+
}
333+
stk.push(node);
334+
}
335+
return stk.getLast();
336+
}
337+
}
338+
```
339+
282340
### **C++**
283341

284342
```cpp
@@ -400,6 +458,43 @@ public:
400458
};
401459
```
402460
461+
```cpp
462+
/**
463+
* Definition for a binary tree node.
464+
* struct TreeNode {
465+
* int val;
466+
* TreeNode *left;
467+
* TreeNode *right;
468+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
469+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
470+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
471+
* };
472+
*/
473+
class Solution {
474+
public:
475+
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
476+
stack<TreeNode*> stk;
477+
for (int v : nums) {
478+
TreeNode* node = new TreeNode(v);
479+
TreeNode* last = nullptr;
480+
while (!stk.empty() && stk.top()->val < v) {
481+
last = stk.top();
482+
stk.pop();
483+
}
484+
node->left = last;
485+
if (!stk.empty()) {
486+
stk.top()->right = node;
487+
}
488+
stk.push(node);
489+
}
490+
while (stk.size() > 1) {
491+
stk.pop();
492+
}
493+
return stk.top();
494+
}
495+
};
496+
```
497+
403498
### **Go**
404499

405500
```go
@@ -523,6 +618,34 @@ func max(a, b int) int {
523618
}
524619
```
525620

621+
```go
622+
/**
623+
* Definition for a binary tree node.
624+
* type TreeNode struct {
625+
* Val int
626+
* Left *TreeNode
627+
* Right *TreeNode
628+
* }
629+
*/
630+
func constructMaximumBinaryTree(nums []int) *TreeNode {
631+
stk := []*TreeNode{}
632+
for _, v := range nums {
633+
node := &TreeNode{Val: v}
634+
var last *TreeNode
635+
for len(stk) > 0 && stk[len(stk)-1].Val < v {
636+
last = stk[len(stk)-1]
637+
stk = stk[:len(stk)-1]
638+
}
639+
node.Left = last
640+
if len(stk) > 0 {
641+
stk[len(stk)-1].Right = node
642+
}
643+
stk = append(stk, node)
644+
}
645+
return stk[0]
646+
}
647+
```
648+
526649
### **C**
527650

528651
```c

0 commit comments

Comments
 (0)