[LeetCode] 654. Maximum Binary Tree

本文介绍了一种基于整数数组构建最大二叉树的方法,详细解析了递归解法及高级解法,通过实例展示了如何寻找数组中的最大值并以此为根节点构建左右子树。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Maximum Binary Tree

Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:

  1. The root is the maximum number in the array.
  2. The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
  3. The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.

Construct the maximum tree by the given array and output the root node of this tree.
在这里插入图片描述

解析

构建一个二叉树,树的根节点是当前列表中最大的值,分别递归构建走右子树,左子树的根节点类似于上面定义,是列表左边的最大值,同理,右子树的根节点是列表右边的最大值。

解法1:递归

找到最大值,建立根节点,再分别在列表左右两边递归建立左右子树。
借用辅助函数可以减少构建新列表的内存。

class Solution {
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        int size = nums.size();
        if(!size) return NULL;
        return helper(nums, 0, size-1);
    }
    
    TreeNode* helper(vector<int>& nums, int start, int end) {
        if(start > end) return NULL;
        int index = start;
        for(int i=start;i<=end;i++){
            if(nums[i] > nums[index]){
                index = i;
            }
        }
        TreeNode* root = new TreeNode(nums[index]);
        root->left = helper(nums, start, index-1);
        root->right = helper(nums, index+1, end);
        return root;
    }
};

不借用辅助函数,每次都要构建两个新的列表。

class Solution {
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        int size = nums.size();
        if(!size) return NULL;
        int index = 0;
        int m = INT_MIN;
        for(int i=0;i<size;i++){
            if(nums[i] > m){
                index = i;
                m = nums[i];
            }
        }        
        TreeNode* root = new TreeNode(m);
        vector<int> leftnode = vector<int>(nums.begin(), nums.begin()+index);
        vector<int> rightnode = vector<int>(nums.begin()+index+1, nums.end());
        root->left = constructMaximumBinaryTree(leftnode);
        root->right = constructMaximumBinaryTree(rightnode);
        return root;
    }
};

解法2:大神写法

大神们使用到了一个辅助数组res来让保持降序。遍历数组,对于每个遍历到的数字,创建一个结点,然后进行循环,如果数组res不空,且末尾结点值小于当前数字,那么将末尾结点连到当前结点的左子结点,并且移除数组中的末尾结点,这样可以保证子结点都会小于父结点。循环结束后,如果此时数组res仍不为空,说明结点值很大,那么将当前结点连到数组末尾结点的右子结点上。之后别忘了将当前结点加入数组res中,最后返回数组res的首结点即可。

class Solution {
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        vector<TreeNode*> res;
        for(int i=0;i<nums.size();i++)
        {
            TreeNode* node = new TreeNode(nums[i]);
            while(!res.empty() && res.back()->val < nums[i])
            {
                node->left = res.back();
                res.pop_back();
            }
            if(!res.empty())
                res.back()->right = node;
            res.push_back(node);
        }
        return res[0];
    }
};

参考

http://www.cnblogs.com/grandyang/p/7513099.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值