654. 最大二叉树
题目链接:654. 最大二叉树
代码如下:
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
private://前序遍历TreeNode* constructTree(vector<int>& nums,int left,int right){if(left>right) return nullptr;//递归出口int maxIndex=left;for(int i=left;i<=right;i++){if(nums[i]>nums[maxIndex])maxIndex=i;}TreeNode* node=new TreeNode(nums[maxIndex]);node->left=constructTree(nums,left,maxIndex-1);node->right=constructTree(nums,maxIndex+1,right);return node;}public:TreeNode* constructMaximumBinaryTree(vector<int>& nums) {return constructTree(nums,0,nums.size()-1);}
};