TreeNode* construct(const vector<int>& nums, int left, int right){ if (left > right) { returnnullptr; } int best = left; for (int i = left + 1; i <= right; ++i) { if (nums[i] > nums[best]) { best = i; } } TreeNode* node = newTreeNode(nums[best]); node->left = construct(nums, left, best - 1); node->right = construct(nums, best + 1, right); return node; } };
public TreeNode Construct(int[] nums, int left, int right) { if (left > right) { returnnull; } int best = left; for (int i = left + 1; i <= right; ++i) { if (nums[i] > nums[best]) { best = i; } } TreeNode node = new TreeNode(nums[best]); node.left = Construct(nums, left, best - 1); node.right = Construct(nums, best + 1, right); return node; } }
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution: defconstructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]: defconstruct(left: int, right: int) -> Optional[TreeNode]: if left > right: returnNone best = left for i inrange(left + 1, right + 1): if nums[i] > nums[best]: best = i node = TreeNode(nums[best]) node.left = construct(left, best - 1) node.right = construct(best + 1, right) return node return construct(0, len(nums) - 1)
var constructMaximumBinaryTree = function(nums) { constconstruct = (nums, left, right) => { if (left > right) { returnnull; } let best = left; for (let i = left + 1; i <= right; ++i) { if (nums[i] > nums[best]) { best = i; } } const node = newTreeNode(nums[best]); node.left = construct(nums, left, best - 1); node.right = construct(nums, best + 1, right); return node; } returnconstruct(nums, 0, nums.length - 1); };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12
funcconstructMaximumBinaryTree(nums []int) *TreeNode { iflen(nums) == 0 { returnnil } best := 0 for i, num := range nums { if num > nums[best] { best = i } } return &TreeNode{nums[best], constructMaximumBinaryTree(nums[:best]), constructMaximumBinaryTree(nums[best+1:])} }
复杂度分析
时间复杂度:O(n^2),其中 n 是数组 nums 的长度。在最坏的情况下,数组严格递增或递减,需要递归 n 层,第 i~(0 \leq i < n) 层需要遍历 n-i 个元素以找出最大值,总时间复杂度为 O(n^2)。
classSolution: defconstructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]: n = len(nums) stk = list() left, right = [-1] * n, [-1] * n tree = [None] * n
for i inrange(n): tree[i] = TreeNode(nums[i]) while stk and nums[i] > nums[stk[-1]]: right[stk[-1]] = i stk.pop() if stk: left[i] = stk[-1] stk.append(i) root = None for i inrange(n): if left[i] == right[i] == -1: root = tree[i] elif right[i] == -1or (left[i] != -1and nums[left[i]] < nums[right[i]]): tree[left[i]].right = tree[i] else: tree[right[i]].left = tree[i] return root
funcconstructMaximumBinaryTree(nums []int) *TreeNode { n := len(nums) left := make([]int, n) right := make([]int, n) for i := range right { right[i] = -1 } tree := make([]*TreeNode, n) stk := []int{-1} for i, num := range nums { tree[i] = &TreeNode{Val: num} forlen(stk) > 1 && num > nums[stk[len(stk)-1]] { right[stk[len(stk)-1]] = i stk = stk[:len(stk)-1] } left[i] = stk[len(stk)-1] stk = append(stk, i) }
var root *TreeNode for i, node := range tree { l, r := left[i], right[i] if l == -1 && r == -1 { root = node } elseif r == -1 || l != -1 && nums[l] < nums[r] { tree[l].Right = node } else { tree[r].Left = node } } return root }
classSolution { public: TreeNode* constructMaximumBinaryTree(vector<int>& nums){ int n = nums.size(); vector<int> stk; vector<TreeNode*> tree(n); for (int i = 0; i < n; ++i) { tree[i] = newTreeNode(nums[i]); while (!stk.empty() && nums[i] > nums[stk.back()]) { tree[i]->left = tree[stk.back()]; stk.pop_back(); } if (!stk.empty()) { tree[stk.back()]->right = tree[i]; } stk.push_back(i); } return tree[stk[0]]; } };
[sol3-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public TreeNode constructMaximumBinaryTree(int[] nums) { intn= nums.length; List<Integer> stack = newArrayList<Integer>(); TreeNode[] tree = newTreeNode[n]; for (inti=0; i < n; ++i) { tree[i] = newTreeNode(nums[i]); while (!stack.isEmpty() && nums[i] > nums[stack.get(stack.size() - 1)]) { tree[i].left = tree[stack.get(stack.size() - 1)]; stack.remove(stack.size() - 1); } if (!stack.isEmpty()) { tree[stack.get(stack.size() - 1)].right = tree[i]; } stack.add(i); } return tree[stack.get(0)]; } }
[sol3-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicclassSolution { public TreeNode ConstructMaximumBinaryTree(int[] nums) { int n = nums.Length; IList<int> stack = new List<int>(); TreeNode[] tree = new TreeNode[n]; for (int i = 0; i < n; ++i) { tree[i] = new TreeNode(nums[i]); while (stack.Count > 0 && nums[i] > nums[stack[stack.Count - 1]]) { tree[i].left = tree[stack[stack.Count - 1]]; stack.RemoveAt(stack.Count - 1); } if (stack.Count > 0) { tree[stack[stack.Count - 1]].right = tree[i]; } stack.Add(i); } return tree[stack[0]]; } }
[sol3-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: defconstructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]: n = len(nums) stk = list() tree = [None] * n
for i inrange(n): tree[i] = TreeNode(nums[i]) while stk and nums[i] > nums[stk[-1]]: tree[i].left = tree[stk[-1]] stk.pop() if stk: tree[stk[-1]].right = tree[i] stk.append(i) return tree[stk[0]]