日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

leetcode654. 最大二叉树

發布時間:2023/12/4 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 leetcode654. 最大二叉树 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一:題目



二:思路

1.這是構造二叉樹,我們一般通過前序遍歷來構造二叉樹
2.我們是通過找到一個最大值,這個最大值的所對應下標的右邊是右子樹,下標的左邊是左子樹
3.通過遞歸構造二叉樹
1>:遞歸函數的參數和返回值
TreeNode* constructMaximumBinaryTree(vector& nums)
(我們返回的是一個指針所以是 TreeNode*類型的)
2>:遞歸函數的終止條件
如果 數組元素就剩下一個元素,那么的話就可以終止了
if(nums.size() == 1) {
node->val = nums[0];//剩下一個元素就是下標為0的元素了
return node;
}
3>:遞歸體
我們每次遞歸需要選出一個最大值和最大值對應的下標因為我們需要進行分割

int maxValue = 0;
int maxValueIndex;
for(int i = 0; i < nums.size(); i++) {
if(nums[i] > maxValue) {
maxValue = nums[i];
macValueIndex = i;
}
}

node->val = maxValue;//中

//遞歸建立左子樹
vector vec1(nums.begin(),nums.begin()+maxValueIndex);
node->left = constructMaximumBinaryTree(vec1);

//遞歸建立右子樹
vector vec2(nums.begin()+maxValueIndex+1,nums.size()-1);
node->right = constructMaximumBinaryTree(vec2);

三:上碼

/*** 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:/**思路:1.這是構造二叉樹,我們一般通過前序遍歷來構造二叉樹2.我們是通過找到一個最大值,這個最大值的所對應下標的右邊是右子樹,下標的左邊是左子樹3.通過遞歸構造二叉樹1>:遞歸函數的參數和返回值TreeNode* constructMaximumBinaryTree(vector<int>& nums)(我們返回的是一個指針所以是 TreeNode*類型的)2>:遞歸函數的終止條件如果 數組元素就剩下一個元素,那么的話就可以終止了if(nums.size() == 1) {node->val = nums[0];//剩下一個元素就是下標為0的元素了return node; } 3>:遞歸體我們每次遞歸需要選出一個最大值和最大值對應的下標因為我們需要進行分割int maxValue = 0;int maxValueIndex;for(int i = 0; i < nums.size(); i++) {if(nums[i] > maxValue) {maxValue = nums[i];macValueIndex = i;}}node->val = maxValue;//中//遞歸建立左子樹vector<int> vec1(nums.begin(),nums.begin()+maxValueIndex);node->left = constructMaximumBinaryTree(vec1);//遞歸建立右子樹vector<int> vec2(nums.begin()+maxValueIndex+1,nums.size()-1);node->right = constructMaximumBinaryTree(vec2); */TreeNode* constructMaximumBinaryTree2(vector<int>& nums) {TreeNode* node = new TreeNode(0);if(nums.size() == 1) {node->val = nums[0];//剩下一個元素就是下標為0的元素了return node; } int maxValue = 0;int maxValueIndex;for(int i = 0; i < nums.size(); i++) {if(nums[i] > maxValue) {maxValue = nums[i];maxValueIndex = i;}}node->val = maxValue;//中//遞歸建立左子樹if(maxValueIndex > 0){//保證左區間至少一個值vector<int> vec1(nums.begin(),nums.begin()+maxValueIndex);node->left = constructMaximumBinaryTree2(vec1);}//遞歸建立右子樹if(maxValueIndex < nums.size()-1){//保證右區間至少一個值 比如nums.size()=5那么下標最大值是4 Index < 4vector<int>vec2(nums.begin()+maxValueIndex+1,nums.end());//那么的話就是右區間至少一個元素node->right = constructMaximumBinaryTree2(vec2); //這樣加if條件就是避免了空節點的導入}return node;}TreeNode* constructMaximumBinaryTree(vector<int>& nums) {return constructMaximumBinaryTree2(nums);} };

總結

以上是生活随笔為你收集整理的leetcode654. 最大二叉树的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。