DAY24
235二叉搜索树的最近公共祖先
迭代法:
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- class Solution {
- public:
- TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
- while(root)
- {
- if(root->val>p->val&&root->val>q->val) root=root->left;
- else if(root->val<p->val&&root->val<q->val) root=root->right;
- else return root;
- }
- return root;;
- }
- };
递归:
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- class Solution {
- public:
- TreeNode* finda(TreeNode* root,TreeNode* p,TreeNode* q)
- {
- if(root==NULL) return root;
- //递归边,所以要用变量接住他,带上if
- if(root->val>p->val&&root->val>q->val)
- {
- TreeNode* left=finda(root->left,p,q);
- if(left!=NULL) return left;
- }
- if(root->val<p->val&&root->val<q->val)
- {
- TreeNode* right=finda(root->right,p,q);
- if(right!=NULL) return right;
- }
- return root;
- }
- TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
- return finda(root,p,q);
- }
- };
总结:
如果递归函数有返回值,如何区分要搜索一条边,还是搜索整个树?
- 搜索一条边:
- 搜素整个树:
本题就是标准的搜索一条边的写法,遇到递归函数的返回值,如果不为空,立刻返回。
701二叉搜索树中的插入操作
solution1:用父节点去接住他(新val)不会写。
找到插入的节点位置,直接让其父节点指向插入节点,结束递归,也是可以的。
- /**
- * 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:
- TreeNode* par;
- void solu1(TreeNode* cur,int val){
- //找到位置了
- if(cur==nullptr){
- TreeNode* node=new TreeNode(val);
- if(par->val>val) par->left=node;
- else par->right=node;
- //加上终止!
- return;
- }
- par=cur;
- //递归函数里面有终止逻辑,这里不用单独接住他
- if(cur->val>val) solu1(cur->left,val);
- if(cur->val<val) solu1(cur->right,val);
- return ;
- }
- public:
- TreeNode* insertIntoBST(TreeNode* root, int val) {
- par=new TreeNode(0);
- if(root==nullptr){
- TreeNode* res=new TreeNode(val);
- return res;
- }
- solu1(root,val);
- return root;
- }
- };
solution2:遇到空,声明新节点并返回,让东西接住他。
- /**
- * 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* solu2(TreeNode* root,int val){
- if(root==nullptr){
- TreeNode* res=new TreeNode(val);
- return res;
- }
- if(root->val>val){
- root->left=solu2(root->left,val);
- }
- if(root->val<val){
- root->right=solu2(root->right,val);
- }
- return root;
- }
- TreeNode* insertIntoBST(TreeNode* root, int val) {
- return solu2(root,val);
- }
- };
自己都没看懂solution2就通过了,递归真神奇难懂。
solution3:迭代法
- /**
- * 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* insertIntoBST(TreeNode* root, int val) {
- if(root==nullptr){
- TreeNode* node=new TreeNode(val);
- return node;
- }
- TreeNode* par=root;
- TreeNode* cur=root;
- while(cur)
- {
- par=cur;
- if(cur->val>val) cur=cur->left;
- else cur=cur->right;
- }
- TreeNode* res=new TreeNode(val);
- if(par->val>val) par->left=res;
- else par->right=res;
- return root;
- }
- };
450删除二叉搜索树中的节点
会做了!
详细见纸质笔记本。对递归的理解更加深刻了。
- /**
- * 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* deleteNode(TreeNode* root, int key) {
- if(root==nullptr) return nullptr;
- if(root->val==key)
- {
- if(root->left==nullptr&&root->right==nullptr) return nullptr;
- else if(root->left!=nullptr&&root->right==nullptr) return root->left;
- else if(root->left==nullptr&&root->right!=nullptr) return root->right;
- else{
- TreeNode* cur=root->right;
- while(cur->left) cur=cur->left;
- cur->left=root->left;
- return root->right;
- }
- }
- if(root->val>key) root->left=deleteNode(root->left,key);
- if(root->val<key) root->right=deleteNode(root->right,key);
- return root;
- }
- };