• 654. 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:

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

    Example 2:

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

    题目:给定数组,创建一个大顶二叉树。要求树顶位置的值大于左右子树的值,而且左右子树也符合这个规则。且在数组中位于树顶元素左边的元素组成左子树,右边的元素组成右子树。

    思路:

    方法一,当然是笨方法,递归,每次找数组中的最大值就好了。代码:

    1. /**
    2. * Definition for a binary tree node.
    3. * struct TreeNode {
    4. * int val;
    5. * TreeNode *left;
    6. * TreeNode *right;
    7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    10. * };
    11. */
    12. class Solution {
    13. public:
    14. TreeNode* buildTree(vector<int>& nums, int start, int end){
    15. if(start > end) return NULL;
    16. if(start == end) return new TreeNode(nums[start]);
    17. int idx = start;
    18. for(int i = start; i <= end; i++){
    19. if(nums[i] > nums[idx]) idx = i;
    20. }
    21. TreeNode* root = new TreeNode(nums[idx]);
    22. root->left = buildTree(nums, start, idx-1);
    23. root->right = buildTree(nums, idx+1, end);
    24. return root;
    25. }
    26. TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
    27. return buildTree(nums, 0, nums.size()-1);
    28. }
    29. };

    时间复杂度:O(N^2), 空间复杂度:递归用到的栈空间不算的话,为O(1)

    方法二,因为数组中每个值都会是树中的一个节点,因此从头往后遍历,每个值都生成一个树节点。用一个栈维护着树结构,栈中树节点的值是单调递减的。即栈维护着每个子树的树顶节点。如遍历到的值比栈顶节点值小,直接加在栈顶节点的右子树上。如果遍历到的值比栈顶节点大,则代表新遍历到的值是栈顶节点的父辈或祖辈~。将栈顶结点pop出来,找到正确位置。pop出的节点位于新节点的左子树上。然后把新节点存进去。代码:

    1. /**
    2. * Definition for a binary tree node.
    3. * struct TreeNode {
    4. * int val;
    5. * TreeNode *left;
    6. * TreeNode *right;
    7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    10. * };
    11. */
    12. class Solution {
    13. public:
    14. TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
    15. stack stk;
    16. for(auto n : nums){
    17. TreeNode* node = new TreeNode(n);
    18. if(!stk.empty()){
    19. if(stk.top()->val > n){
    20. stk.top()->right = node;
    21. } else {
    22. TreeNode* top = stk.top();
    23. while(!stk.empty() && stk.top()->val < n){
    24. top = stk.top();
    25. stk.pop();
    26. }
    27. node->left = top;
    28. if(!stk.empty())
    29. stk.top()->right = node;
    30. }
    31. }
    32. stk.push(node);
    33. }
    34. while(stk.size() > 1) stk.pop();
    35. return stk.top();
    36. }
    37. };

    时间复杂度:O(N), 空间复杂度: O(N)

  • 相关阅读:
    MODBUS通信浮点数存储解析常用算法
    简答一波 HashMap 常见八股面试题 —— 算法系列(2)
    第三方模块远程注入到软件中引发软件异常的若干实战案例分享
    为什么程序开发中不推荐使用全局变量?
    定时重启指定的软件
    Guava工具
    flutter开发实战 - inappwebview设置cookie
    【计算机网络】Tomcat和Servlet基础知识汇总
    火狐浏览器翻译页面功能如何设置
    (二十七)大数据实战——hbase高可用集群安装与部署
  • 原文地址:https://blog.csdn.net/qing2019/article/details/126171999