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

DAY24

235二叉搜索树的最近公共祖先

迭代法:

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

递归:

  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8.  * };
  9.  */
  10. class Solution {
  11. public:
  12.     TreeNode* finda(TreeNode* root,TreeNode* p,TreeNode* q)
  13.     {
  14.         if(root==NULLreturn root;
  15.         //递归边,所以要用变量接住他,带上if
  16.         if(root->val>p->val&&root->val>q->val)
  17.         {
  18.             TreeNode* left=finda(root->left,p,q);
  19.             if(left!=NULLreturn left;
  20.         }
  21.         if(root->val<p->val&&root->val<q->val)
  22.         {
  23.             TreeNode* right=finda(root->right,p,q);
  24.             if(right!=NULLreturn right;
  25.         }
  26.         return root;
  27.     }
  28.     TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
  29.         return finda(root,p,q);
  30.     }
  31. };

总结:

如果递归函数有返回值,如何区分要搜索一条边,还是搜索整个树?

  • 搜索一条边:

  • 搜素整个树:

本题就是标准的搜索一条边的写法,遇到递归函数的返回值,如果不为空,立刻返回。

701二叉搜索树中的插入操作

solution1:用父节点去接住他(新val)不会写。

找到插入的节点位置,直接让其父节点指向插入节点,结束递归,也是可以的。

  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.     TreeNode* par;
  15.     void solu1(TreeNode* cur,int val){
  16.         //找到位置了
  17.         if(cur==nullptr){
  18.             TreeNode* node=new TreeNode(val);
  19.             if(par->val>val) par->left=node;
  20.             else par->right=node;
  21.             //加上终止!
  22.             return;
  23.         }
  24.         par=cur;
  25.         //递归函数里面有终止逻辑,这里不用单独接住他
  26.         if(cur->val>val) solu1(cur->left,val);
  27.         if(cur->val<val) solu1(cur->right,val);
  28.         return ;
  29.     }
  30. public
  31.     TreeNode* insertIntoBST(TreeNode* root, int val) {
  32.         par=new TreeNode(0);
  33.         if(root==nullptr){
  34.              TreeNode* res=new TreeNode(val);
  35.              return res;
  36.         }
  37.         solu1(root,val);
  38.         return root;
  39.     }
  40. };

solution2:遇到空,声明新节点并返回,让东西接住他。

  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* solu2(TreeNode* root,int val){
  15.         if(root==nullptr){
  16.             TreeNode* res=new TreeNode(val);
  17.             return res;
  18.         }
  19.         if(root->val>val){
  20.             root->left=solu2(root->left,val);
  21.         }
  22.         if(root->val<val){
  23.             root->right=solu2(root->right,val);
  24.         }
  25.         return root;
  26.     }
  27.     TreeNode* insertIntoBST(TreeNode* root, int val) {
  28.         return solu2(root,val);
  29.     }
  30. };

自己都没看懂solution2就通过了,递归真神奇难懂。

solution3:迭代法

  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* insertIntoBST(TreeNode* root, int val) {
  15.         if(root==nullptr){
  16.             TreeNode* node=new TreeNode(val);
  17.             return node;
  18.         }
  19.         TreeNode* par=root;
  20.         TreeNode* cur=root;
  21.         while(cur)
  22.         {
  23.             par=cur;
  24.             if(cur->val>val) cur=cur->left;
  25.             else  cur=cur->right;
  26.         }
  27.         TreeNode* res=new TreeNode(val);
  28.         if(par->val>val) par->left=res;
  29.         else par->right=res;
  30.         return root;
  31.     }
  32. };

450删除二叉搜索树中的节点

会做了!

详细见纸质笔记本。对递归的理解更加深刻了。

  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* deleteNode(TreeNode* root, int key) {
  15.         if(root==nullptr) return nullptr;
  16.         if(root->val==key)
  17.         {
  18.             if(root->left==nullptr&&root->right==nullptr) return nullptr;
  19.             else if(root->left!=nullptr&&root->right==nullptr) return root->left;
  20.             else if(root->left==nullptr&&root->right!=nullptr) return root->right;
  21.             else{
  22.                 TreeNode* cur=root->right;
  23.                 while(cur->left) cur=cur->left;
  24.                 cur->left=root->left;
  25.                 return root->right;
  26.             }
  27.         }
  28.         if(root->val>key) root->left=deleteNode(root->left,key);
  29.         if(root->val<key) root->right=deleteNode(root->right,key);
  30.         return root;
  31.     }
  32. };

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值