/** * 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 { public: TreeNode* search(vector<int>& nums, int left, int right){ if(left >= right) return nullptr; int index = left; for(int i=left+1; i<right; i++) if(nums[i] > nums[index]) index = i; TreeNode* root = new TreeNode(nums[index]); root->left = search(nums, left, index); root->right = search(nums, index+1, right); return root; } TreeNode* constructMaximumBinaryTree(vector<int>& nums) { return search(nums, 0, nums.size()); } };
标签:index,right,TreeNode,nums,int,day20,part06,二叉树,left From: https://www.cnblogs.com/zqh2023/p/17673666.html