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

代码随想录算法训练营Day13

110.平衡二叉树

力扣题目链接:. - 力扣(LeetCode)

后序迭代

class Solution {public boolean isBalanced(TreeNode root) {return getHeight(root)!=-1;}public int getHeight(TreeNode root){if(root==null){return 0;}int leftheight=getHeight(root.left);int rightheith=getHeight(root.right);if(leftheight==-1||rightheith==-1){return -1;}else if(Math.abs(leftheight-rightheith)>1){return -1;  }return Math.max(leftheight,rightheith)+1;}
}

257. 二叉树的所有路径

力扣题目链接:. - 力扣(LeetCode)

前序递归

class Solution {public List<String> binaryTreePaths(TreeNode root) {List<String> res=new ArrayList<>();if(root==null){return res;}List<Integer> pathnode=new ArrayList<>();bianli(root,res,pathnode);return res;}public void bianli(TreeNode root,List<String> res,List<Integer> pathnode){pathnode.add(root.val);if(root.left==null&&root.right==null){StringBuilder str=new StringBuilder();for(int i=0;i<pathnode.size()-1;i++){str.append(pathnode.get(i)).append("->");}str.append(pathnode.get(pathnode.size()-1));res.add(str.toString());return;}if(root.left!=null){bianli(root.left,res,pathnode);if(!pathnode.isEmpty()){pathnode.remove(pathnode.size()-1);}       }if(root.right!=null){bianli(root.right,res,pathnode);if(!pathnode.isEmpty()){pathnode.remove(pathnode.size()-1);}     }return;}
}

404.左叶子之和

力扣题目链接:. - 力扣(LeetCode)

后序递归

class Solution {public int sumOfLeftLeaves(TreeNode root) {if(root==null){return 0;}int rightsum=sumOfLeftLeaves(root.right);int leftsum=sumOfLeftLeaves(root.left);int midsum=0;if(root.left!=null&&root.left.left==null&&root.left.right==null){midsum=root.left.val;}int sum=midsum+rightsum+leftsum;return sum;}
}

222.完全二叉树的节点个数

力扣题目链接:. - 力扣(LeetCode)

层序遍历

class Solution {public int countNodes(TreeNode root) {if(root==null){return 0;}Deque<TreeNode> myque=new LinkedList<>();myque.offer(root);int sum=0;int res=0;while(!myque.isEmpty()){sum++;int len=myque.size();res+=len;if(len!=Math.pow(2,sum-1))break;while(len>0){TreeNode cur=myque.poll();if(cur.left!=null){myque.offer(cur.left);}if(cur.right!=null){myque.offer(cur.right);}len--;}}return res;}
}

相关文章:

  • 标准 I/O
  • pg入门11-pg中的publications是什么
  • 【移植】Combo解决方案之W800芯片移植案例
  • 『功能项目』鼠标悬停物品显示信息【77】
  • .Net 6.0 Windows平台如何判断当前电脑是否联网
  • 重头开始嵌入式第四十四天(硬件 ARM裸机开发)
  • 外国电影演员识别系统源码分享
  • 当大语言模型应用到教育领域时会有什么火花出现?
  • SD(Stable Diffusion)模型的基本工作数据流
  • 批量发送邮件:性能优化与错误处理深度解析
  • 基于微信小程序爱心领养小程序设计与实现(源码+定制+开发)
  • 算法刷题笔记 约数个数(详细注释的C++实现)
  • 【Java】单元测试【主线学习笔记】
  • 通俗易懂的Latex使用步骤
  • RNA-seq通用代码-生物信息学pipeline001
  • 【跃迁之路】【699天】程序员高效学习方法论探索系列(实验阶段456-2019.1.19)...
  • Angular 响应式表单 基础例子
  • axios 和 cookie 的那些事
  •  D - 粉碎叛乱F - 其他起义
  • docker-consul
  • HTTP 简介
  • JavaScript工作原理(五):深入了解WebSockets,HTTP/2和SSE,以及如何选择
  • java中具有继承关系的类及其对象初始化顺序
  • JS基础篇--通过JS生成由字母与数字组合的随机字符串
  • js继承的实现方法
  • Objective-C 中关联引用的概念
  • pdf文件如何在线转换为jpg图片
  • Python语法速览与机器学习开发环境搭建
  • spring boot下thymeleaf全局静态变量配置
  • Three.js 再探 - 写一个跳一跳极简版游戏
  • Vue 动态创建 component
  • vue从创建到完整的饿了么(18)购物车详细信息的展示与删除
  • 浮动相关
  • 类orAPI - 收藏集 - 掘金
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 使用putty远程连接linux
  • 在weex里面使用chart图表
  • 怎样选择前端框架
  • 《TCP IP 详解卷1:协议》阅读笔记 - 第六章
  • 7行Python代码的人脸识别
  • 策略 : 一文教你成为人工智能(AI)领域专家
  • ​​​​​​​ubuntu16.04 fastreid训练过程
  • #include到底该写在哪
  • (1)Hilt的基本概念和使用
  • (175)FPGA门控时钟技术
  • (Forward) Music Player: From UI Proposal to Code
  • (k8s)kubernetes集群基于Containerd部署
  • (Oracle)SQL优化技巧(一):分页查询
  • (八)c52学习之旅-中断实验
  • (编译到47%失败)to be deleted
  • (二)测试工具
  • (论文阅读22/100)Learning a Deep Compact Image Representation for Visual Tracking
  • (删)Java线程同步实现一:synchronzied和wait()/notify()
  • (转载)(官方)UE4--图像编程----着色器开发
  • * 论文笔记 【Wide Deep Learning for Recommender Systems】