240 发简信
IP属地:伊利诺伊州
  • Hi Jason,

    According to your excellent observation,
    "通过观察发现规律,对于每个node的父亲节点 = min(左边第一个比它大的,右边第一个比它大的)"

    It can be directly implemented this way

    ```
    class Solution {
    public:
    /**
    * @param A: Given an integer array with no duplicates.
    * @return: The root of max tree.
    */
    TreeNode * maxTree(vector<int> &A) {
    // write your code here
    //stack<TreeNode *> s;
    A.push_back(INT_MAX);
    stack<TreeNode *> s;
    for (int i = 0; i < A.size(); ++i)
    {
    TreeNode *node = new TreeNode(A[i]);
    while (!s.empty() && A[i] >= s.top()->val)
    {
    TreeNode * cur = s.top();
    s.pop();
    if (s.empty() || s.top()->val > A[i])
    {
    node->left = cur;
    }
    else
    {
    s.top()->right = cur;
    }
    }
    s.push(node);
    }

    return s.top()->left;
    }

    };
    ```

    LintCode 126 [Max Tree]

    原题 给出一个没有重复的整数数组,在此数组上建立最大树的定义如下:1.根是数组中最大的数2.左子树和右子树元素分别是被父节点元素切分开的子数组中的最大值利用给定的数组构造最大...

  • LintCode 126 [Max Tree]

    原题 给出一个没有重复的整数数组,在此数组上建立最大树的定义如下:1.根是数组中最大的数2.左子树和右子树元素分别是被父节点元素切分开的子数组中的最大值利用给定的数组构造最大...