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

松江建设机械网站哪些做海报比较好的网站

松江建设机械网站,哪些做海报比较好的网站,网站举报查询进度,php网站文件夹结构数据结构 二叉树OJ题 文章目录 数据结构 二叉树OJ题1. 检查两颗二叉树是否相同2. 判断树是否为另一个树的子树3. 翻转二叉树4. 平衡二叉树5. 对称二叉树6. 二叉树遍历7. 二叉树层序遍历8. 最近公共祖先9. 二叉树创建字符串10. 非递归方式实现前序遍历11. 非递归方式实现中序遍历…数据结构 二叉树OJ题 文章目录 数据结构 二叉树OJ题1. 检查两颗二叉树是否相同2. 判断树是否为另一个树的子树3. 翻转二叉树4. 平衡二叉树5. 对称二叉树6. 二叉树遍历7. 二叉树层序遍历8. 最近公共祖先9. 二叉树创建字符串10. 非递归方式实现前序遍历11. 非递归方式实现中序遍历12. 非递归方式实现后序遍历 结合之前所学的二叉树知识我们来刷一些OJ题巩固一下 1. 检查两颗二叉树是否相同 OJ链接 代码示例 class Solution {public boolean isSameTree(TreeNode p,TreeNode q) {if ((p null q ! null) || (p ! null q null)) {return false;}if (p null q null) {return true;}if (p.val ! q.val) {return false;}return isSameTree(p.left,q.left) isSameTree(p.right,q.right);} }2. 判断树是否为另一个树的子树 OJ链接 代码示例 class Solution {public boolean isSameTree(TreeNode p,TreeNode q) {if ((p null q ! null) || (p ! null q null)) {return false;}if (p null q null) {return true;}if (p.val ! q.val) {return false;}return isSameTree(p.left,q.left) isSameTree(p.right,q.right);}public boolean isSubtree(TreeNode root,TreeNode subRoot) {if (root null || subRoot null) {return false;}if (isSameTree(root,subRoot)) {return true;}if (isSubtree(root.left,subRoot)) {return true;}if (isSubtree(root.right,subRoot)) {return true;}return false;} }3. 翻转二叉树 OJ链接 代码示例 class Solution {public TreeNode invertTree(TreeNode root) {if (root null) {return null;}if (root.left null root.right null) {return root;}TreeNode temp root.left;root.left root.right;root.right temp;invertTree(root.left);invertTree(root.right);return root;} }4. 平衡二叉树 OJ链接 代码示例 class Solution {public boolean isBalanced(TreeNode root) {if (root null) {return true;}int leftH getHeight(root.left);int rightH getHeight(root.right);if (Math.abs(leftH-rightH) 1) {return false;}return isBalanced(root.left) isBalanced(root.right);}public int getHeight(TreeNode root) {if (root null) {return 0;}int leftNode getHeight(root.left);int rightNode getHeight(root.right);return leftNode rightNode ? leftNode 1 : rightNode 1;} }5. 对称二叉树 OJ链接 代码示例 class Solution {public boolean isSymmetric(TreeNode root) {if (root null) {return true;}return isSymmetricChild(root.left,root.right);}private boolean isSymmetricChild(TreeNode leftTree, TreeNode rightTree) {if (leftTree null rightTree null) {return true;}if ((leftTree null rightTree ! null) || (leftTree ! null rightTree null)) {return false;}if (leftTree.val ! rightTree.val) {return false;}return isSymmetricChild(leftTree.left,rightTree.right) isSymmetricChild(leftTree.right,rightTree.left);} }6. 二叉树遍历 [OJ链接]: 代码示例 import java.util.Scanner;public class Main {public static int i 0;static class TreeNode {char val;TreeNode left;TreeNode right;public TreeNode(char val) {this.val val;}}public static void main(String[] args) {Scanner in new Scanner(System.in);String s1 in.nextLine();TreeNode root createTree(s1);isOrder(root);}public static TreeNode createTree(String s) {if (s.charAt(i) ! #) {TreeNode root new TreeNode(s.charAt(i));i;root.left createTree(s);root.right createTree(s);return root;}else {i;}return null;}public static void isOrder(TreeNode root) {if (root null) {return;}isOrder(root.left);System.out.print(root.val );isOrder(root.right);} }7. 二叉树层序遍历 OJ链接 代码示例 class Solution {public ListListInteger levelOrder(TreeNode root) {ListListInteger ret1 new LinkedList();QueueTreeNode queue new LinkedList();if (root null) {return ret1;}queue.offer(root);while(!queue.isEmpty()) {ListInteger list new LinkedList();int size queue.size();while(size ! 0) {TreeNode cur queue.poll();size--;System.out.print(cur.val );if (cur.left ! null) {queue.offer(cur.left);}if (cur.right ! null) {queue.offer(cur.right);}list.add(cur.val);}ret1.add(list);}return ret1;} }8. 最近公共祖先 OJ链接 代码示例 class Solution {public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {if (root null) {return null;}if (root p || root q) {return root;}TreeNode leftTree lowestCommonAncestor(root.left,p,q);TreeNode rightTree lowestCommonAncestor(root.right,p,q);if (leftTree ! null rightTree ! null) {return root;}else if(leftTree ! null) {return leftTree;}else {return rightTree;}} }9. 二叉树创建字符串 [OJ链接]: 代码示例 class Solution {public String tree2str(TreeNode root) {StringBuilder stringBuilder new StringBuilder();tree2strChild(root,stringBuilder);return stringBuilder.toString();}private void tree2strChild(TreeNode root, StringBuilder stringBuilder) {if (root null) {return;}stringBuilder.append(root.val );if (root.left ! null) {stringBuilder.append(();tree2strChild(root.left,stringBuilder);stringBuilder.append());}else {if (root.right null) {return;}else{stringBuilder.append(());}}if (root.right ! null) {stringBuilder.append(();tree2strChild(root.right,stringBuilder);stringBuilder.append());}else {return;}} } 10. 非递归方式实现前序遍历 [OJ链接]: 代码示例 class Solution {public ListInteger preorderTraversal(TreeNode root) {ListInteger list new LinkedList();if (root null) {return list;}StackTreeNode stack new Stack();TreeNode cur root;while(cur ! null || !stack.isEmpty()) {while(cur ! null) {stack.push(cur);list.add(cur.val);cur cur.left;}TreeNode top stack.pop();cur top.right;}return list;}}11. 非递归方式实现中序遍历 [OJ链接]: 代码示例 class Solution {public ListInteger inorderTraversal(TreeNode root) {ListInteger list new LinkedList();if (root null) {return list;}StackTreeNode stack new Stack();TreeNode cur root;while(cur ! null || !stack.empty()) {while(cur ! null) {stack.push(cur);cur cur.left;}TreeNode top stack.pop();list.add(top.val);cur top.right;}return list;} }12. 非递归方式实现后序遍历 [OJ链接]: 代码示例 class Solution {public ListInteger postorderTraversal(TreeNode root) {ListInteger list new LinkedList();if (root null) {return list;}StackTreeNode stack new Stack();TreeNode cur root;TreeNode prev null;while(cur ! null || !stack.isEmpty()) {while(cur ! null) {stack.push(cur);cur cur.left;}TreeNode top stack.peek();if (top.right null || top.right prev) {list.add(top.val);stack.pop();prev top; // 记录下最新被打印的节点}else {cur top.right;}}return list;} }
http://www.sadfv.cn/news/58376/

相关文章:

  • 网站开发官网谷歌搜索引擎入口google
  • 淘宝联盟做的好的网站wordpress 导航菜单添加
  • 济南企业做网站推广网站网站建设目的功能
  • 好看的公司网站排版设计衡水安徽网站建设
  • 网站可以做2个公司的吗广州网络建站
  • 东莞南城网站开发公司电话网站开发做前端还是后端
  • 人设生成器网站太原论坛天涯社区
  • 电影网站设计模板制作一个网页的教程
  • 单片机程序员开发网站网站后台密码忘记了
  • 沈阳网站建设开发如何增加网站索引量
  • 网站运营与管理的目的是装潢设计属于什么专业
  • 潍坊 seo网站建设页面设计的像胶囊怎么形容
  • 学设计的网课沧州网站排名优化
  • 文山州住房和城乡建设局网站百度一下首页官网百度
  • 某企业网站建设论文快速建站官网
  • 做网站用的三角形图片有没有免费手游代理
  • 外贸网站运营小程序制作页面教程
  • 环保网站建设维护情况报告吕梁建设机械网站
  • 山东济宁网站建设温州互联网公司
  • 用电脑建设个人网站 并用手机访问seo排名赚app官网
  • 营销型外贸网站建设成都高端响应式网站开发
  • 购物网站建设基本流程树状图网站建设项目需求分析流程
  • 专业建设物流行业网站专业外贸网站建设公司
  • 网站页面优化工具wordpress上传都图片不显示
  • 建设工程竞标网站凡科做网站要钱
  • 园林景观网站源码wordpress 更改数据表
  • 动态ip如何做网站汉中网站建设价格
  • 商城的网站统计如何做公司起名字大全免费测吉凶
  • 网站建设历史安徽博物馆网站建设的调研报告
  • 服务网站设计案例海外互联网推广平台