|
52 | 52 | <li><code>nums</code> 中的所有整数 <strong>互不相同</strong></li>
|
53 | 53 | </ul>
|
54 | 54 |
|
55 |
| - |
56 | 55 | ## 解法
|
57 | 56 |
|
58 | 57 | <!-- 这里可写通用的实现逻辑 -->
|
59 | 58 |
|
| 59 | +先找到数组的最大元素所在的位置,作为根节点,然后递归左右两侧的子数组,构建左右子树。 |
| 60 | + |
60 | 61 | <!-- tabs:start -->
|
61 | 62 |
|
62 | 63 | ### **Python3**
|
63 | 64 |
|
64 | 65 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
65 | 66 |
|
66 | 67 | ```python
|
67 |
| - |
| 68 | +# Definition for a binary tree node. |
| 69 | +# class TreeNode: |
| 70 | +# def __init__(self, val=0, left=None, right=None): |
| 71 | +# self.val = val |
| 72 | +# self.left = left |
| 73 | +# self.right = right |
| 74 | +class Solution: |
| 75 | + def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode: |
| 76 | + def inner(nums, l, r): |
| 77 | + if l > r: |
| 78 | + return None |
| 79 | + mx = l |
| 80 | + for i in range(l + 1, r + 1): |
| 81 | + if nums[mx] < nums[i]: |
| 82 | + mx = i |
| 83 | + return TreeNode(nums[mx], inner(nums, l, mx - 1), inner(nums, mx + 1, r)) |
| 84 | + |
| 85 | + return inner(nums, 0, len(nums) - 1) |
68 | 86 | ```
|
69 | 87 |
|
70 | 88 | ### **Java**
|
71 | 89 |
|
72 | 90 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
73 | 91 |
|
74 | 92 | ```java
|
| 93 | +/** |
| 94 | + * Definition for a binary tree node. |
| 95 | + * public class TreeNode { |
| 96 | + * int val; |
| 97 | + * TreeNode left; |
| 98 | + * TreeNode right; |
| 99 | + * TreeNode() {} |
| 100 | + * TreeNode(int val) { this.val = val; } |
| 101 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 102 | + * this.val = val; |
| 103 | + * this.left = left; |
| 104 | + * this.right = right; |
| 105 | + * } |
| 106 | + * } |
| 107 | + */ |
| 108 | +class Solution { |
| 109 | + public TreeNode constructMaximumBinaryTree(int[] nums) { |
| 110 | + return construct(nums, 0, nums.length - 1); |
| 111 | + } |
| 112 | + |
| 113 | + private TreeNode construct(int[] nums, int l, int r) { |
| 114 | + if (l > r) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + int mx = l; |
| 118 | + for (int i = l + 1; i <= r; ++i) { |
| 119 | + if (nums[mx] < nums[i]) { |
| 120 | + mx = i; |
| 121 | + } |
| 122 | + } |
| 123 | + return new TreeNode(nums[mx], construct(nums, l, mx - 1), construct(nums, mx + 1, r)); |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +### **C++** |
| 129 | + |
| 130 | +```cpp |
| 131 | +/** |
| 132 | + * Definition for a binary tree node. |
| 133 | + * struct TreeNode { |
| 134 | + * int val; |
| 135 | + * TreeNode *left; |
| 136 | + * TreeNode *right; |
| 137 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 138 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 139 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 140 | + * }; |
| 141 | + */ |
| 142 | +class Solution { |
| 143 | +public: |
| 144 | + TreeNode* constructMaximumBinaryTree(vector<int>& nums) { |
| 145 | + return construct(nums, 0, nums.size() - 1); |
| 146 | + } |
| 147 | + |
| 148 | + TreeNode* construct(vector<int>& nums, int l, int r) { |
| 149 | + if (l > r) return nullptr; |
| 150 | + int mx = l; |
| 151 | + for (int i = l + 1; i <= r; ++i) { |
| 152 | + if (nums[mx] < nums[i]) mx = i; |
| 153 | + } |
| 154 | + TreeNode* root = new TreeNode(nums[mx]); |
| 155 | + root->left = construct(nums, l, mx - 1); |
| 156 | + root->right = construct(nums, mx + 1, r); |
| 157 | + return root; |
| 158 | + } |
| 159 | +}; |
| 160 | +``` |
75 | 161 |
|
| 162 | +### **Go** |
| 163 | + |
| 164 | +```go |
| 165 | +/** |
| 166 | + * Definition for a binary tree node. |
| 167 | + * type TreeNode struct { |
| 168 | + * Val int |
| 169 | + * Left *TreeNode |
| 170 | + * Right *TreeNode |
| 171 | + * } |
| 172 | + */ |
| 173 | +func constructMaximumBinaryTree(nums []int) *TreeNode { |
| 174 | + return construct(nums, 0, len(nums)-1) |
| 175 | +} |
| 176 | + |
| 177 | +func construct(nums []int, l, r int) *TreeNode { |
| 178 | + if l > r { |
| 179 | + return nil |
| 180 | + } |
| 181 | + mx := l |
| 182 | + for i := l + 1; i <= r; i++ { |
| 183 | + if nums[mx] < nums[i] { |
| 184 | + mx = i |
| 185 | + } |
| 186 | + } |
| 187 | + return &TreeNode{ |
| 188 | + Val: nums[mx], |
| 189 | + Left: construct(nums, l, mx-1), |
| 190 | + Right: construct(nums, mx+1, r), |
| 191 | + } |
| 192 | +} |
76 | 193 | ```
|
77 | 194 |
|
78 | 195 | ### **...**
|
|
0 commit comments