当前位置: 首页 > 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;}
}

http://www.lryc.cn/news/451026.html

相关文章:

  • 基于STM32的智能门禁系统
  • [EBPF] 实时捕获DM数据库是否存在SQL阻塞
  • 秋招内推--招联金融2025
  • Unity2022.3.x各个版本bug集合及推荐稳定版本
  • SparkSQL-性能调优
  • leetcode-链表篇
  • JetLinks物联网平台微服务化系列文章介绍
  • 【QT Quick】基础语法:导入外部QML文件
  • Llama 系列简介与 Llama3 预训练模型推理
  • 【AIGC】ChatGPT提示词助力自媒体内容创作升级
  • SSTI基础
  • 10.1软件工程知识详解上
  • 03Frenet与Cardesian坐标系(Frenet转Cardesian公式推导)
  • knowLedge-Vue I18n 是 Vue.js 的国际化插件
  • 【开源免费】基于SpringBoot+Vue.JS微服务在线教育系统(JAVA毕业设计)
  • expressjs 中的mysql.createConnection,execute 怎么使用
  • 每日一题|983. 最低票价|动态规划、记忆化递归
  • oracle 正则 匹配 身份正 手机号
  • 在树莓派上部署开源监控系统 ZoneMinder
  • 2022年6月 Frontier 获得性能第一的论文翻译
  • B2B商城交易解决方案:赋能企业有效重塑采购与销售新生态
  • 初始C语言(五)
  • mysql学习教程,从入门到精通,SQL 修改表(ALTER TABLE 语句)(29)
  • 【网络基础】网络常识快速入门知识清单,看这篇文章就够了
  • OceanBase 关于一号表笔记与ERROR 1060(42S21)问题
  • 【四】Spring Cloud OpenFeign原理分析
  • EDM平台大比拼 用户体验与营销效果双重测评
  • 开卷可扩展自动驾驶(OpenDriveLab)
  • 基于大数据的二手电子产品需求分析及可视化系统
  • SpringBoot——基础配置