654. Maximum Binary Tree
Last updated
Last updated
Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:
The root is the maximum number in the array.
The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
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:
左邊最大的就會是 node.left,右邊最大的就會是 node.right 用 recursive 的方式讓相同的邏輯重複去指定,直到結尾
用 stack 的方式,按照順序存入,不停更新 原本在右邊的一定會是(比較小時) node.right 或是(比較大時)其上方的 parent-node,原本在左邊的也是同理。