654. Maximum Binary Tree 最大二元樹
❀ Origin
Problem
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 | Input: [3,2,1,6,0,5] |
Note
- The size of the given array will be in the range [1,1000].
❀ 翻譯
問題
給定一個不重複的整數陣列, 一個以此陣列建立的最大二元樹的定義如下:
- 根節點為其陣列中的最大值
- 左子樹是得出的最大值的左邊範圍所建構出的最大二元樹
- 右子樹是得出的最大值的右邊範圍所建構出的最大二元樹
使用給定的陣列建構出最大二元樹, 並輸出該樹的根節點
注意
- 給定陣列的長度範圍為 1 ~ 1000 .
❀ Solution
Idea
- 建立抓最大值的 function
- 建立產生最大二元樹的 function
- 對其執行遞迴
JavaScript
1 | /** |
Execution
1 | //JavaScript |