码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Maximum Binary Tree

时间:2018-01-15 12:32:41      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:maximum   represent   struct   from   instead   divide   amp   gpo   binary   

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.

Example 1:

Input: [3,2,1,6,0,5]
Output: return the tree root node representing the following tree:

      6
    /      3     5
    \    / 
     2  0   
               1

Note:

  1. The size of the given array will be in the range [1,1000].

给定一个元素不重复的数组,构建一个由该数组组成的最大树,这个最大树要求如下:

1、根节点是数组中的最大数

2、左子树的根节点由这个最大数分割数组的左边部分的最大数组成。

3、右子树的根节点由这个最大数分割数组的右边部分的最大数组成。

构建这个树并返回这个树的根节点。

 

方法1:使用迭代的方法。

使用一个vector来模拟栈存储数组节点。

如果当前数组为空,则将当前节点直接入栈

如果当前数组不为空,比较当前节点curNode与nodeVec->back()的val的大小。

1、如果当前数组非空并且当前节点值大于数组尾元素。则说明数组尾元素是当前节点的左子节点。此时弹出数组中元素。

2、如果当前元素非空,则说明当前节点为数组尾元素的右子节点。

将当前节点放入数组中。

最后返回数组中的头元素。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        vector<TreeNode*> nodeVec;
        for (int i = 0; i < nums.size(); i++) {
            TreeNode* curNode = new TreeNode(nums[i]);
            if (nodeVec.empty()) {
                nodeVec.push_back(curNode);
            }
            else {
                while (!nodeVec.empty() && nodeVec.back()->val < curNode->val) {
                    curNode->left = nodeVec.back();
                    nodeVec.pop_back();
                }
                if (!nodeVec.empty()) {
                    nodeVec.back()->right = curNode;
                }
                nodeVec.push_back(curNode);
            }
        }
        return nodeVec.front();
    }
};
// 65 ms

 方法2:使用递归的方法

对于每一步

1、找出最大元素

2、根据最大元素将数组分为两部分

3、将最大元素的左右节点迭代分配

返回根节点

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        if (nums.empty())
            return nullptr;
        
        // use auto to declare iterator
        auto it = max_element(nums.begin(), nums.end());
        int maxVal = *it;
        
        // use auto to init with new; use nullptr instead of NULL
        auto* node = new TreeNode(maxVal);
        vector<int> left_nums(nums.begin(), it);
        vector<int> right_nums(it + 1, nums.end());
        node->left = constructMaximumBinaryTree(left_nums);
        node->right = constructMaximumBinaryTree(right_nums);
        
        return node;
    }
};
// 74 ms

 

[LeetCode] Maximum Binary Tree

标签:maximum   represent   struct   from   instead   divide   amp   gpo   binary   

原文地址:https://www.cnblogs.com/immjc/p/8287358.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!