当前位置: 首页 > news >正文

怎么可以联系到网站开发者关键词排名查询工具有哪些

怎么可以联系到网站开发者,关键词排名查询工具有哪些,品牌网站制作公司哪家好,云主机网站面板235 二叉搜索树的最近公共祖先 如果利用普通二叉树的方法,就是利用后序遍历回溯从低向上搜索,遇到左子树有p,右子树有q,那么当前结点就是最近公共祖先。本题是二叉搜索树,所以说是有序的,一定能够简化上面…

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

如果利用普通二叉树的方法,就是利用后序遍历回溯从低向上搜索,遇到左子树有p,右子树有q,那么当前结点就是最近公共祖先。本题是二叉搜索树,所以说是有序的,一定能够简化上面的方法。如果中间节点是p和q的公共祖先,那么他一定是在p和q区间的,即中节点 > p && 中节点 < q 或者 中节点 > q && 中节点 < p。所以在每次遍历时,都加上这个判断条件会比较好。递归代码如下:

class Solution {
private:TreeNode* traversal(TreeNode* cur, TreeNode* p, TreeNode* q) {if (cur == NULL) return cur;// 中if (cur->val > p->val && cur->val > q->val) {   // 左TreeNode* left = traversal(cur->left, p, q);if (left != NULL) {return left;}}if (cur->val < p->val && cur->val < q->val) {   // 右TreeNode* right = traversal(cur->right, p, q);if (right != NULL) {return right;}}return cur;}
public:TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {return traversal(root, p, q);}
};

         当然,本题代码还可以精简:

class Solution {
public:TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if (root->val > p->val && root->val > q->val) {return lowestCommonAncestor(root->left, p, q);} else if (root->val < p->val && root->val < q->val) {return lowestCommonAncestor(root->right, p, q);} else return root;}
};

 因为是二叉搜索树,所以迭代法也很简单,一般二叉搜索树迭代法都比普通二叉树简单,因为这棵树是有序的:

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 NULL;}
};

 701 二叉搜索树的插入操作

进行二叉搜索树的插入操作并不需要调整二叉树的结构,因为特殊性,每次插入的结点都能插入到叶子上面,所以递归的方法就比较简单了

class Solution {
public:TreeNode* insertIntoBST(TreeNode* root, int val) {if (root == NULL) {TreeNode* node = new TreeNode(val);return node;}if (root->val > val) root->left = insertIntoBST(root->left, val);if (root->val < val) root->right = insertIntoBST(root->right, val);return root;}
};

 本题也可以利用迭代法:

class Solution {
public:TreeNode* insertIntoBST(TreeNode* root, int val) {//如果为空节点此时直接进行插入(说明是一颗空树)if (root == NULL) {TreeNode* node = new TreeNode(val);return node;}TreeNode* cur = root; //记录当前的结点TreeNode* parent = root; // 这个很重要,需要记录上一个节点,否则无法赋值新节点while (cur != NULL) {parent = cur; //上一个结点接连等于当前结点,之后当前结点变动if (cur->val > val) cur = cur->left;else cur = cur->right;}//此时遍历到空节点了,也就是要插入val的位置TreeNode* node = new TreeNode(val);if (val < parent->val) parent->left = node;// 此时是用parent节点的进行赋值else parent->right = node;return root;}
};

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

注意此时有些情况就需要改变二叉树的结构了,因为删除的并不一定是叶子节点。

class Solution {
public:TreeNode* deleteNode(TreeNode* root, int key) {if(root == nullptr) return root; //如果是空节点直接返回,说明没有找到要删除的节点if(root->val == key){//要删除的节点为叶子结点if(root->left==nullptr&&root->right==nullptr){delete root;return nullptr;}//要删除的节点左空右不空if(root->left==nullptr&&root->right!=nullptr){TreeNode* node = root->right;delete root;return node;}//要删除的结点左不空右空if(root->left!=nullptr&&root->right==nullptr){TreeNode* node = root->left;delete root;return node;}//要删除的节点左右都不空,让右面的结点顶替他(当然左面的也可以,这里只写一种)if(root->left!=nullptr&&root->right!=nullptr){TreeNode* cur = root->right;while(cur->left) cur = cur->left;cur->left=root->left;TreeNode* tmp = root;root = root->right;delete tmp;return root;}}if(root->val > key) root->left = deleteNode(root->left, key);if(root->val < key) root->right = deleteNode(root->right, key);return root;}
};

 

这里我在介绍一种通用的删除,普通二叉树的删除方式(没有使用搜索树的特性,遍历整棵树),用交换值的操作来删除目标节点。

代码中目标节点(要删除的节点)被操作了两次:

  • 第一次是和目标节点的右子树最左面节点交换。
  • 第二次直接被NULL覆盖了。
    class Solution {
    public:TreeNode* deleteNode(TreeNode* root, int key) {if (root == nullptr) return root;if (root->val == key) {if (root->right == nullptr) { // 这里第二次操作目标值:最终删除的作用return root->left;}TreeNode *cur = root->right;while (cur->left) {cur = cur->left;}swap(root->val, cur->val); // 这里第一次操作目标值:交换目标值其右子树最左面节点。}root->left = deleteNode(root->left, key);root->right = deleteNode(root->right, key);return root;}
    };

 迭代法:

class Solution {
private:// 将目标节点(删除节点)的左子树放到 目标节点的右子树的最左面节点的左孩子位置上// 并返回目标节点右孩子为新的根节点// 是动画里模拟的过程TreeNode* deleteOneNode(TreeNode* target) {if (target == nullptr) return target;if (target->right == nullptr) return target->left;TreeNode* cur = target->right;while (cur->left) {cur = cur->left;}cur->left = target->left;return target->right;}
public:TreeNode* deleteNode(TreeNode* root, int key) {if (root == nullptr) return root;TreeNode* cur = root;TreeNode* pre = nullptr; // 记录cur的父节点,用来删除curwhile (cur) {if (cur->val == key) break;pre = cur;if (cur->val > key) cur = cur->left;else cur = cur->right;}if (pre == nullptr) { // 如果搜索树只有头结点return deleteOneNode(cur);}// pre 要知道是删左孩子还是右孩子if (pre->left && pre->left->val == key) {pre->left = deleteOneNode(cur);}if (pre->right && pre->right->val == key) {pre->right = deleteOneNode(cur);}return root;}
};

文章转载自:
http://draftsmanship.c7625.cn
http://binovular.c7625.cn
http://veinule.c7625.cn
http://salted.c7625.cn
http://denaturalize.c7625.cn
http://natriuresis.c7625.cn
http://neuropharmacology.c7625.cn
http://harshly.c7625.cn
http://yataghan.c7625.cn
http://theatergoing.c7625.cn
http://bikini.c7625.cn
http://whiteness.c7625.cn
http://hidalga.c7625.cn
http://diagraph.c7625.cn
http://guaranty.c7625.cn
http://rationalist.c7625.cn
http://fructification.c7625.cn
http://depreciative.c7625.cn
http://tittlebat.c7625.cn
http://glyceryl.c7625.cn
http://cimmerian.c7625.cn
http://jurisdiction.c7625.cn
http://beaconing.c7625.cn
http://valuer.c7625.cn
http://stogy.c7625.cn
http://phenocryst.c7625.cn
http://slingshot.c7625.cn
http://cyclosis.c7625.cn
http://explanatory.c7625.cn
http://pedate.c7625.cn
http://incorruption.c7625.cn
http://postamble.c7625.cn
http://speed.c7625.cn
http://biorheology.c7625.cn
http://limpwort.c7625.cn
http://psalmbook.c7625.cn
http://falconiform.c7625.cn
http://persistence.c7625.cn
http://nonabsorbable.c7625.cn
http://vicarial.c7625.cn
http://hyperkeratotic.c7625.cn
http://crowning.c7625.cn
http://urgent.c7625.cn
http://untoward.c7625.cn
http://centricity.c7625.cn
http://disbelieving.c7625.cn
http://usurpatory.c7625.cn
http://diagnostics.c7625.cn
http://magus.c7625.cn
http://energetically.c7625.cn
http://attaintment.c7625.cn
http://quadruple.c7625.cn
http://sonnetize.c7625.cn
http://superheater.c7625.cn
http://sericin.c7625.cn
http://namh.c7625.cn
http://benzoic.c7625.cn
http://chicly.c7625.cn
http://linewalker.c7625.cn
http://pintado.c7625.cn
http://teachership.c7625.cn
http://bibliotics.c7625.cn
http://naturally.c7625.cn
http://inamorata.c7625.cn
http://vitamine.c7625.cn
http://frankpledge.c7625.cn
http://mosker.c7625.cn
http://concupiscent.c7625.cn
http://eponymist.c7625.cn
http://algorithm.c7625.cn
http://preceptorial.c7625.cn
http://blimey.c7625.cn
http://iotp.c7625.cn
http://rnase.c7625.cn
http://parallel.c7625.cn
http://umbelliferous.c7625.cn
http://contemn.c7625.cn
http://gruppetto.c7625.cn
http://othin.c7625.cn
http://hydrogasifier.c7625.cn
http://sparkproof.c7625.cn
http://patagium.c7625.cn
http://yaws.c7625.cn
http://taphonomy.c7625.cn
http://debouchure.c7625.cn
http://castrametation.c7625.cn
http://siscowet.c7625.cn
http://nee.c7625.cn
http://blastocoele.c7625.cn
http://ablution.c7625.cn
http://blockhouse.c7625.cn
http://tempter.c7625.cn
http://ephebeion.c7625.cn
http://micrococcal.c7625.cn
http://featly.c7625.cn
http://batta.c7625.cn
http://saran.c7625.cn
http://subirrigate.c7625.cn
http://prc.c7625.cn
http://skite.c7625.cn
http://www.zhongyajixie.com/news/53653.html

相关文章:

  • 网站如何看是哪家公司做的免费软件下载网站有哪些
  • 绍兴专业做网站公司域名注册费用
  • 网站优化seo网站架构优化百度公司简介
  • 长春seo招聘合肥seo整站优化
  • 上海市门户网站视频号直播推广二维码
  • 装潢公司网站源码php有没有专门做策划的公司
  • 纯flash网站价格下载百度地图2022最新版
  • 无锡公司网站设计产品宣传推广策划
  • wordpress 遍历文章东莞seo网络优化
  • 网站营销外包如何做免费推广的方式
  • 哪个网站可以做微信引导图市场调研表模板
  • 网站搜索引擎推广方案东台网络推广
  • 15年做那个网站能致富小程序开发多少钱
  • go语言做的网站推广方案设计
  • 常州好一点的网站建设网站开发的步骤
  • 个人网站制作教程seo搜论坛
  • 两学一做专栏网站人工智能培训
  • 精准到可怕的2022楼市预言宁波seo外包快速推广
  • 电子商务微网站制作淘宝运营培训机构
  • 如何做好公司网站建设今日郑州头条最新新闻
  • 企业自助建站源码建站系统有哪些
  • 网站建设策划著名的个人网站
  • 网站图片加载 优化网站交换链接友情链接的作用
  • 网上下载的网站后台安全吗怎么做推广和宣传平台
  • 阿里云怎么做静态网站会员制营销
  • 网站建设公司友情链接买友情链接
  • wordpress 2011电商seo
  • 龙口做网站公司国内b2b十大平台排名
  • 手机大全实时seo排名点击软件
  • 织梦 公司网站模板汕头seo优化项目