zoukankan      html  css  js  c++  java
  • [LeetCode] 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.

    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
  • 相关阅读:
    Makefile的介绍与使用(一)
    关于OpenWRT第一次编译时出现的一些问题
    ssh连接时出现 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! 解决办法
    ArcGIS 添加存WKT字符串的字段
    已连接的 RDBMS 实例未针对 Esri 空间类型配置进行相应设置
    ArcGIS10.2 安装好后,数据库连接的配置
    用Excel将中文转成大驼峰拼音
    将 Python 项目的所有py文件编译成.pyc
    地图要素增删改服务WFS: Openlayer+GeoServer+GeoPackage
    ArcGIS 和 QGIS 经常崩溃闪退的原因
  • 原文地址:https://www.cnblogs.com/immjc/p/8287358.html
Copyright © 2011-2022 走看看