DAY22
654最大二叉樹
自己做的時候忽略了:nums.length>=1的題給條件。所以每次遞歸都要判斷是否size()>=1,不要空的。
- /**
- ?*?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*?constructMaximumBinaryTree(vector<int>&?nums)?{
- ????????TreeNode*?node=new?TreeNode(0);
- ????????if(nums.size()==1)?{
- ????????????node->val=nums[0];
- ????????????return?node;
- ????????}
- ????????int?index=0;
- ????????for(int?i=1;i<nums.size();i++)
- ????????{
- ????????????if(nums[index]<nums[i])?index=i;
- ????????}
- ????????node->val=nums[index];
- ????????if(index>0){
- ????????????vector<int>?ln(nums.begin(),nums.begin()+index);
- ????????????node->left=constructMaximumBinaryTree(ln);
- ????????}
- ????????if(index<nums.size()-1){
- ????????????vector<int>?rn(nums.begin()+index+1,nums.end());
- ????????????node->right=constructMaximumBinaryTree(rn);
- ????????}
- ????????return?node;
- ????}
- };
617合并二叉樹
遞歸之前,先弄明白遍歷順序很重要:
- /**
- ?*?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*?mergeTrees(TreeNode*?root1,?TreeNode*?root2)?{
- ????????if(root1==nullptr)?return?root2;
- ????????if(root2==nullptr)?return?root1;
- ????????root1->val+=root2->val;
- ????????root1->left=mergeTrees(root1->left,root2->left);
- ????????root1->right=mergeTrees(root1->right,root2->right);
- ????????return?root1;
- ????}
- };
700二叉搜索樹中的搜索
先寫迭代法,返璞歸真咯:
- /**
- ?*?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*?searchBST(TreeNode*?root,?int?val)?{
- ????????while(root!=nullptr)
- ????????{
- ????????????if(root->val<val)?root=root->right;
- ????????????else?if(root->val>val)?root=root->left;
- ????????????else?return?root;
- ????????}
- ????????return?nullptr;
- ????}
- };
遞歸試試:
遞歸還是寫不出來,早上看過答案也寫不出來,邏輯理不清,什么時候該創建變量去接住遞歸的結果?
- /**
- ?*?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*?searchBST(TreeNode*?root,?int?val)?{
- ????????if(root==nullptr||root->val==val)?return?root;
- ????????TreeNode*?result=nullptr;
- ????????if(root->val<val)?{
- ????????????result=searchBST(root->right,val);
- ????????}
- ????????if(root->val>val){
- ????????????result=searchBST(root->left,val);
- ????????}
- ????????return?result;
- ????}
- };
遞歸沒接住(沒找到),就返回的是初始化的result=nullptr;
98驗證二叉搜索樹
有很多陷阱在里面。迭代法就很容易出錯。
這里要知道并利用這個性質:二叉搜索樹的中序遍歷序列是嚴格單調增的數組。
- /**
- ?*?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:
- ????vector<int>?result;
- ????void?exc(TreeNode?*root)
- ????{
- ????????if(root==nullptr)?return;
- ????????exc(root->left);
- ????????result.push_back(root->val);
- ????????exc(root->right);
- ????}
- public:
- ????bool?isValidBST(TreeNode*?root)?{
- ????????result.clear();
- ????????exc(root);
- ????????for(int?i=1;i<result.size();i++)
- ????????{
- ????????????if(result[i-1]>=result[i])?return?false;
- ????????}
- ????????return?true;
- ????}
- };