代码随想录算法训练营Day13

news/2024/9/29 1:18:41/

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.ppmy.cn/news/1531378.html

相关文章

PPT 快捷键使用、技巧

前言&#xff1a; 本文操作是以office 2021为基础的&#xff0c;仅供参考&#xff1b;不同版本office 的 ppt 快捷键 以及对应功能会有差异&#xff0c;需要实践出真知。 shift 移动 水平/垂直 移动 &#xff1b; shift 放大/缩小 等比例放大 缩小 &#xff1b; 正圆 正…

协议IP规定,576字节和1500字节的区别

576字节和1500字节的区别主要在于它们是IP数据报在数据链路层中的最大传输单元&#xff08;MTU&#xff09;的不同限制。‌ ‌576字节‌&#xff1a;这个数值通常与IP层&#xff08;网络层&#xff09;的数据报有关&#xff0c;它指的是在不进行分片的情况下&#xff0c;IP数据…

【Android】布局优化—include,merge,ViewStub的使用方法

引言 1.重要性 在Android应用开发中&#xff0c;布局是用户界面的基础。一个高效的布局不仅能提升用户体验&#xff0c;还能显著改善应用的性能。随着应用功能的复杂性增加&#xff0c;布局的优化变得尤为重要。优化布局能够减少渲染时间&#xff0c;提高响应速度&#xff0c…

MySQL(面试问题)

MySQL用了什么索引结构 B 树 B树的优势 对比B树&#xff0c;B树的数据放在叶子节点&#xff0c;非叶子节点存放索引&#xff0c;而B树中&#xff0c;行数据放在非叶子节点中。相比之下&#xff0c;B一次读入的索引节点更多&#xff0c;减少IO次数&#xff0c;缩短查询时间。 对…

浙大数据结构:05-树9 Huffman Codes

这道题难度挺大&#xff0c;写起来较为费劲&#xff0c;这里我依然使用了STL库&#xff0c;使得代码量大幅减少不过百行&#xff0c;便于大家理解。 机翻&#xff1a; 1、条件准备 数组存储字符对应频率&#xff0c;n,student存储输入多少字符&#xff0c;有多少学生测试。 …

集群系统架构

ShedLock 是一个用于分布式环境下的锁机制&#xff0c;确保在同一时间点只有一个节点能够执行特定的定时任务。其核心原理是通过公共存储&#xff08;如数据库、Redis 等&#xff09;来实现锁的管理12。 具体来说&#xff0c;当一个任务在某个节点上开始执行时&#xff0c;该节…

node-red-L3-重启指定端口的 node-red

重启指定端口 目的步骤查找正在运行的Node.js服务的进程ID&#xff08;PID&#xff09;&#xff1a;停止Node.js服务&#xff1a;启动Node.js服务&#xff1a; 目的 重启指定端口的 node-red 步骤 在Linux系统中&#xff0c;如果你想要重启一个正在运行的Node.js服务&#x…

cubemx配置ADC

参考博客&#xff1a;https://blog.csdn.net/qq_29031103/article/details/119894077 生成代码&#xff1b; 接着编写代码&#xff1a; 1&#xff09;在main函数bsp初始化部分&#xff1b; 添加&#xff1a;HAL_ADCEx_Calibration_Start(&hadc1); 2&#xff09;在…