[LeetCode] 654. Maximum Binary Tree

Posted arcsinw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 654. 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.右子树是数组中最大值右边部分形成的最大树

看到题就想到了递归

nums的最大数将nums分成了两部分,这两部分作为参数继续调用constructMaximumBinaryTree方法, 把结果拼成一棵二叉树就行了

代码如下

TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
    if (nums.empty())
    {
        return NULL;
    }

    vector<int>::iterator max_iter = max_element(nums.begin(), nums.end());
    TreeNode* root = new TreeNode(*max_iter);

    vector<int> left_vec(nums.begin(), max_iter);
    vector<int> right_vec(max_iter + 1, nums.end());

    root->left = constructMaximumBinaryTree(left_vec);
    root->right = constructMaximumBinaryTree(right_vec);

    return root;
}

再看看leetcode上其他人的代码

只需遍历一次

node指向当前最大的节点,也就是根节点

先把node设为nums的第一个元素,从nums的第二个元素(设为tmp)开始遍历

如果tmp比当前的node节点大,就将当前的node节点作为新的根节点的左节点,node指向新的根节点

如果tmp比当前的node节点小,从当前的node节点的右子树找到第一个比tmp大的节点

TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
    auto it = nums.begin();
    TreeNode* node = new TreeNode(*it);
    TreeNode* tmp = NULL;
    TreeNode* t = NULL;
    for( ++it; it != nums.end(); ++it){
        if (*it > node->val){
            tmp = node;
            node = new TreeNode(*it);
            node->left = tmp;
        }
        else{
            tmp = new TreeNode(*it);
            t = node;
            while(t->right && t->right->val > tmp->val)
                t = t->right;
            tmp->left = t->right;
            t->right = tmp;
        }
    }
    return node;
}
3 2 1 6 0 5

3

2在3的右边,所以在右子树,1也是同理
3
   2
       1

6比3大,3,2,1都在6的左边
    6
   /
  3
       2
           1

0在6的右边且比0小
    6
   /   3   0
       2
           1

插入5的比较关键,先从根节点6开始向右边找到最后一个比5小的数,也就是0,把0变成5的左节点,再将5变成6的右节点

     6
   /     3     5
      /
    2 0
           1

多举个例子表明是要找的是最后一个比要插入的值大的节点

8 6 4 5

    8
           6
               4

接下来插入5,根节点8的右子树中最后一个比5大的节点是6,所以5要为6的右节点(最大树中越高的节点越大)
    8
           6   5
        /
        4  

    8
           6
               5
       /
      4




以上是关于[LeetCode] 654. Maximum Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 654. Maximum Binary Tree

LeetCode - 654. Maximum Binary Tree

[LeetCode] 654. Maximum Binary Tree

[LeetCode] 654. Maximum Binary Tree

[LeetCode] 654. Maximum Binary Tree 最大二叉树

[Leetcode]654.Maximum Binary Tree