LeetCode - Medium - 654. Maximum Binary Tree

痛定思痛。 2022-10-17 14:00 286阅读 0赞

Topic

  • Tree

Description

https://leetcode.com/problems/maximum-binary-tree/

You are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using the following algorithm:

  1. Create a root node whose value is the maximum value in nums.
  2. Recursively build the left subtree on the subarray prefix to the left of the maximum value.
  3. Recursively build the right subtree on the subarray suffix to the right of the maximum value.
    Return the maximum binary tree built from nums.

Example 1:

b29cadd77dc8885a770726468eec2d67.png

  1. Input: nums = [3,2,1,6,0,5]
  2. Output: [6,3,5,null,2,0,null,null,1]
  3. Explanation: The recursive calls are as follow:
  4. - The largest value in [3,2,1,6,0,5] is 6. Left prefix is [3,2,1] and right suffix is [0,5].
  5. - The largest value in [3,2,1] is 3. Left prefix is [] and right suffix is [2,1].
  6. - Empty array, so no child.
  7. - The largest value in [2,1] is 2. Left prefix is [] and right suffix is [1].
  8. - Empty array, so no child.
  9. - Only one element, so child is a node with value 1.
  10. - The largest value in [0,5] is 5. Left prefix is [0] and right suffix is [].
  11. - Only one element, so child is a node with value 0.
  12. - Empty array, so no child.

Example 2:

6bd1e20db72784d568f327a28f14a9ee.png

  1. Input: nums = [3,2,1]
  2. Output: [3,null,2,null,1]

Constraints:

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] <= 1000
  • All integers in nums are unique.

Analysis

方法一:用到后序遍历模式+暴力查找出最大值。

方法二:很巧妙的方法,类似用到中序遍历模式。link

在这里插入图片描述
另外,中序遍历这生成后的树,能得到输入那种序列。

Submission

  1. import java.util.LinkedList;
  2. import com.lun.util.BinaryTree.TreeNode;
  3. public class MaximumBinaryTree {
  4. public TreeNode constructMaximumBinaryTree(int[] nums) {
  5. return constructMaximumBinaryTree(nums, 0, nums.length - 1);
  6. }
  7. private TreeNode constructMaximumBinaryTree(int[] nums, int start, int end) {
  8. int maxValueIndex = findMax(nums, start, end);
  9. if(maxValueIndex == -1) return null;
  10. return new TreeNode(nums[maxValueIndex], //
  11. constructMaximumBinaryTree(nums, start, maxValueIndex - 1), //
  12. constructMaximumBinaryTree(nums, maxValueIndex + 1, end));
  13. }
  14. private int findMax(int[] nums, int start, int end) {
  15. if(start > end) return -1;
  16. int maxValueIndex = start;
  17. for(int i = start + 1; i <= end; i++) {
  18. if(nums[i] > nums[maxValueIndex])
  19. maxValueIndex = i;
  20. }
  21. return maxValueIndex;
  22. }
  23. public TreeNode constructMaximumBinaryTree2(int[] nums) {
  24. LinkedList<TreeNode> stack = new LinkedList<>();
  25. for(int i = 0; i < nums.length; i++) {
  26. TreeNode curr = new TreeNode(nums[i]);
  27. while(!stack.isEmpty() && stack.peek().val < nums[i]) {
  28. curr.left = stack.pop();
  29. }
  30. if(!stack.isEmpty()) {
  31. stack.peek().right = curr;
  32. }
  33. stack.push(curr);
  34. }
  35. return stack.isEmpty() ? null : stack.removeLast();
  36. }
  37. }

Test

  1. import static org.junit.Assert.*;
  2. import org.junit.Test;
  3. import com.lun.util.BinaryTree;
  4. import com.lun.util.BinaryTree.TreeNode;
  5. public class MaximumBinaryTreeTest {
  6. @Test
  7. public void test() {
  8. MaximumBinaryTree obj = new MaximumBinaryTree();
  9. TreeNode actual1 = obj.constructMaximumBinaryTree(new int[] { 3, 2, 1, 6, 0, 5});
  10. TreeNode expected1 = BinaryTree.integers2BinaryTree(6, 3, 5, null, 2, 0, null, null, 1);
  11. assertTrue(BinaryTree.equals(actual1, expected1));
  12. TreeNode actual2 = obj.constructMaximumBinaryTree(new int[] { 3, 2, 1});
  13. TreeNode expected2 = BinaryTree.integers2BinaryTree(3, null, 2, null, 1);
  14. assertTrue(BinaryTree.equals(actual2, expected2));
  15. }
  16. @Test
  17. public void test2() {
  18. MaximumBinaryTree obj = new MaximumBinaryTree();
  19. TreeNode actual1 = obj.constructMaximumBinaryTree2(new int[] { 3, 2, 1, 6, 0, 5});
  20. TreeNode expected1 = BinaryTree.integers2BinaryTree(6, 3, 5, null, 2, 0, null, null, 1);
  21. assertTrue(BinaryTree.equals(actual1, expected1));
  22. TreeNode actual2 = obj.constructMaximumBinaryTree2(new int[] { 3, 2, 1});
  23. TreeNode expected2 = BinaryTree.integers2BinaryTree(3, null, 2, null, 1);
  24. assertTrue(BinaryTree.equals(actual2, expected2));
  25. }
  26. }

发表评论

表情:
评论列表 (有 0 条评论,286人围观)

还没有评论,来说两句吧...

相关阅读