代码随想录算法训练营第36期DAY22

DAY22

654最大二叉树

自己做的时候忽略了:nums.length>=1的题给条件。所以每次递归都要判断是否size()>=1,不要空的。

  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10.  * };
  11.  */
  12. class Solution {
  13. public:
  14.     TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
  15.         TreeNode* node=new TreeNode(0);
  16.         if(nums.size()==1) {
  17.             node->val=nums[0];
  18.             return node;
  19.         }
  20.         int index=0;
  21.         for(int i=1;i<nums.size();i++)
  22.         {
  23.             if(nums[index]<nums[i]) index=i;
  24.         }
  25.         node->val=nums[index];
  26.         if(index>0){
  27.             vector<intln(nums.begin(),nums.begin()+index);
  28.             node->left=constructMaximumBinaryTree(ln);
  29.         }
  30.         if(index<nums.size()-1){
  31.             vector<intrn(nums.begin()+index+1,nums.end());
  32.             node->right=constructMaximumBinaryTree(rn);
  33.         }
  34.         return node;
  35.     }
  36. };

617合并二叉树

递归之前,先弄明白遍历顺序很重要:

  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10.  * };
  11.  */
  12. class Solution {
  13. public:
  14.     TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
  15.         if(root1==nullptr) return root2;
  16.         if(root2==nullptr) return root1;
  17.         root1->val+=root2->val;
  18.         root1->left=mergeTrees(root1->left,root2->left);
  19.         root1->right=mergeTrees(root1->right,root2->right);
  20.         return root1;
  21.     }
  22. };

700二叉搜索树中的搜索

先写迭代法,返璞归真咯:

  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10.  * };
  11.  */
  12. class Solution {
  13. public:
  14.     TreeNode* searchBST(TreeNode* root, int val) {
  15.         while(root!=nullptr)
  16.         {
  17.             if(root->val<val) root=root->right;
  18.             else if(root->val>val) root=root->left;
  19.             else return root;
  20.         }
  21.         return nullptr;
  22.     }
  23. };

递归试试:

递归还是写不出来,早上看过答案也写不出来,逻辑理不清,什么时候该创建变量去接住递归的结果?

  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10.  * };
  11.  */
  12. class Solution {
  13. public:
  14.     TreeNode* searchBST(TreeNode* root, int val) {
  15.         if(root==nullptr||root->val==valreturn root;
  16.         TreeNode* result=nullptr;
  17.         if(root->val<val) {
  18.             result=searchBST(root->right,val);
  19.         }
  20.         if(root->val>val){
  21.             result=searchBST(root->left,val);
  22.         }
  23.         return result;
  24.     }
  25. };

递归没接住(没找到),就返回的是初始化的result=nullptr;

98验证二叉搜索树

有很多陷阱在里面。迭代法就很容易出错。

这里要知道并利用这个性质:二叉搜索树的中序遍历序列是严格单调增的数组。

  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10.  * };
  11.  */
  12. class Solution {
  13. private:
  14.     vector<int> result;
  15.     void exc(TreeNode *root)
  16.     {
  17.         if(root==nullptrreturn;
  18.         exc(root->left);
  19.         result.push_back(root->val);
  20.         exc(root->right);
  21.     }
  22. public:
  23.     bool isValidBST(TreeNode* root) {
  24.         result.clear();
  25.         exc(root);
  26.         for(int i=1;i<result.size();i++)
  27.         {
  28.             if(result[i-1]>=result[i]) return false;
  29.         }
  30.         return true;
  31.     }
  32. };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值