单调栈
654.最大二叉树
https://leetcode.cn/problems/maximum-binary-tree/description/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public TreeNode constructMaximumBinaryTree(int[] nums) { Deque<TreeNode> stack = new LinkedList<>();
for(int n : nums) { TreeNode node = new TreeNode(n);
while(!stack.isEmpty() && n > stack.peek().val) node.left = stack.pop(); if(!stack.isEmpty()) stack.peek().right = node; stack.push(node); } return stack.removeLast(); } }
|