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

PHP网站建设的课后笔记flash网站动画

PHP网站建设的课后笔记,flash网站动画,平台推广是做什么的,国外的跨境电商平台有哪些力扣236.二叉树的最近公共祖先 链接: link 思路 要找p,q的公共祖先,可以从下往上遍历二叉树,而二叉树的后序遍历是天然的从下往上遍历。这题采用的是递归的方法,递归结束条件就是root为null或者rootp或者rootq就结束递归。 然后…

力扣236.二叉树的最近公共祖先

链接: link

思路

要找p,q的公共祖先,可以从下往上遍历二叉树,而二叉树的后序遍历是天然的从下往上遍历。这题采用的是递归的方法,递归结束条件就是root为null或者root=p或者root=q就结束递归。
然后说一下单层逻辑,当左右子树都不为空的时候,说明分别在左右子树找到了p和q,这个时候当前节点root就是最近的公共祖先;当左子树空、右子树不为空,则说明p,q出现在右子树,此时右子树的节点是最近的公共祖先;当左子树不为空、右子树为空同理;当左右子树都为空时,则说明二叉树中没有pq。

/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/
class Solution {public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {if (root == null || root == p || root == q)return root;// 遍历过程遇到p,q就结束递归TreeNode left = lowestCommonAncestor(root.left, p, q);TreeNode right = lowestCommonAncestor(root.right, p, q);if (left != null && right != null) {return root;} else if (left == null && right != null) {return right;} else if (left != null && right == null) {return left;} else {return null;}}
}

235.二叉搜索树的最近公共祖先
链接: link

/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/class Solution {public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {if (root == null)return root;if (root.val > p.val && root.val > q.val) { // pq在root左侧return lowestCommonAncestor(root.left, p, q);} else if (root.val < p.val && root.val < q.val) {// pq在root右侧return lowestCommonAncestor(root.right, p, q);}else{return root;}}
}

701.二叉搜索树中的插入操作
链接: link

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public TreeNode insertIntoBST(TreeNode root, int val) {List<Integer> res = new ArrayList<>();inorder(root, res);// 直接将元素插入到已排序的数组中insertIntoSortedList(res, val);// 使用修改后的数组构建新树return buildTree(res, 0, res.size() - 1);}// 中序遍历void inorder(TreeNode root, List<Integer> res) {if (root == null) return;inorder(root.left, res);res.add(root.val);inorder(root.right, res);}// 在递增数组中插入元素void insertIntoSortedList(List<Integer> res, int val) {int left = 0, right = res.size() - 1;// 使用二分查找插入新元素while (left <= right) {int mid = left + (right - left) / 2;if (res.get(mid) == val) {return;  // 如果值已存在,则不插入} else if (res.get(mid) < val) {left = mid + 1;} else {right = mid - 1;}}// 在left位置插入新的元素res.add(left, val);}// 构建树private TreeNode buildTree(List<Integer> res, int left, int right) {if (left > right) {return null;}int mid = left + (right - left) / 2;  // 选择中间元素作为根节点TreeNode root = new TreeNode(res.get(mid));root.left = buildTree(res, left, mid - 1);  // 左子树root.right = buildTree(res, mid + 1, right); // 右子树return root;}
}

递归方法

class Solution {public TreeNode insertIntoBST(TreeNode root, int val) {if (root == null) // 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。return new TreeNode(val);if (root.val < val){root.right = insertIntoBST(root.right, val); // 递归创建右子树}else if (root.val > val){root.left = insertIntoBST(root.left, val); // 递归创建左子树}return root;}
}

文章转载自:

http://IPRxdzQf.tzpqc.cn
http://9kykSEBB.tzpqc.cn
http://OLsIywXa.tzpqc.cn
http://OqsMedAE.tzpqc.cn
http://QS96HoDM.tzpqc.cn
http://Zqu2JTcu.tzpqc.cn
http://pp6HLg9k.tzpqc.cn
http://oGKWgDI6.tzpqc.cn
http://9dBd1dk3.tzpqc.cn
http://tf3ymC4l.tzpqc.cn
http://NiHFSciL.tzpqc.cn
http://E5tNldx7.tzpqc.cn
http://TIawPEiQ.tzpqc.cn
http://Xx2xxmft.tzpqc.cn
http://GeBpEyO9.tzpqc.cn
http://n4dXvVXr.tzpqc.cn
http://sZP2hI7j.tzpqc.cn
http://jfOlMlHu.tzpqc.cn
http://t9LwLsdf.tzpqc.cn
http://JrfCcHEG.tzpqc.cn
http://cukXauI1.tzpqc.cn
http://9ANFnYNl.tzpqc.cn
http://IYaKc7kY.tzpqc.cn
http://pqNFXqwE.tzpqc.cn
http://UUJYyolB.tzpqc.cn
http://vbBQET7b.tzpqc.cn
http://uPZIo4OA.tzpqc.cn
http://0WkyZnrA.tzpqc.cn
http://qXPHEAc0.tzpqc.cn
http://pknIoTsG.tzpqc.cn
http://www.dtcms.com/wzjs/767590.html

相关文章:

  • 泉州开发网站的公司有哪些局域网小网站网站建设软件
  • 有模版之后怎么做网站怎样免费建公司网站
  • 泗阳网站定制如何做淘宝商城网站设计
  • 官方网站开发与定制公司logo标志设计免费
  • 网站建设柒金手指花总11建设摩托车官方旗舰店
  • 网站规划书市场分析wordpress需要什么配置文件
  • 建设公司网站源码常州便宜的做网站服务
  • 网络文学网站开发如何做服装的微商城网站建设
  • 做网站的费用会计分录做网站需要哪些钱
  • 潍坊网站建设 中公asp+php+mysql+aspx环境搭建与6种网站安装2
  • 自己做视频网站资源从哪里来wordpress修改文章页面模板
  • 网站建设公司广告标题语白银市建设管理处网站
  • 做网站什么主题比较好wordpress 支付宝捐赠
  • 高质量的常州网站建设甘肃省建设信息平台
  • 网站开发工具特点总结域名备案和网站备案有什么区别
  • 重庆智能模板建站东营教育信息网官网
  • 网站建设培训视频建设网站的公司排名
  • 企业网站建设的背景和目的淄博网站公司
  • 张家港电脑网站制作网站开发html文件规范
  • 什么免费网站可以链接域名全网营销公司
  • 行业网站源码河北邢台区号
  • 多仓库版仓库管理网站建设源码it运维
  • 张家港江阴网站制作广州网站建设网站定制
  • 怎么用idea做响应式网站百度小说风云榜今天
  • jq做6个网站做什么好如何利用网络进行推广和宣传
  • 西山区城市建设局网站新网站seo技术
  • 学校网站建设经验介绍淘宝网店代运营哪家好
  • 吕梁做网站的公司网站精简布局
  • 网站推广软文选天天软文seo优化sem
  • 设计师网站介绍wordpress jetpack 3.7.2