基础知识

二叉树的多种遍历方式,每种遍历方式各有其特点

LeetCode 104.二叉树的最大深度

分析1.0

往下遍历深度++,往上回溯深度--

class Solution {
int deep = 0, max = 0;
public int maxDepth(TreeNode root) {
preOrder(root);
return max;
}
void preOrder(TreeNode p){
if(p == null){
return;
}
deep++;
max = Math.max(deep, max);
preOrder(p.left);
preOrder(p.right);
deep--;
}
}

分析2.0

这个思路值得背诵

class solution {
/**
* 递归法
*/
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
return Math.max(leftDepth, rightDepth) + 1;
}
}

LeetCode 111.二叉树的最小深度

分析1.0

同最大深度一样的考虑,每次求最小值,最小值只能在叶节点取得

class Solution {
int deep = 0, min = 100001;
public int minDepth(TreeNode root) {
if(root == null){
return 0;
}
preOrder(root);
return min;
}
void preOrder(TreeNode p){
if(p == null){
return;
}
deep++;
if(p.left == null && p.right == null){
min = Math.min(deep, min);
}
preOrder(p.left);
preOrder(p.right);
deep--;
}
}

求最小值结果变量要初始化为数据集的最大值,求最大值要初始化为数据集的最小值

分析2.0

class Solution {
/**
* 递归法,相比求MaxDepth要复杂点
* 因为最小深度是从根节点到最近**叶子节点**的最短路径上的节点数量
*/
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
int leftDepth = minDepth(root.left);
int rightDepth = minDepth(root.right);
if (root.left == null) {
return rightDepth + 1;
}
if (root.right == null) {
return leftDepth + 1;
}
// 左右结点都不为null
return Math.min(leftDepth, rightDepth) + 1;
}
}

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

分析1.0

完全二叉树求节点个数,知道层数+最后一层节点数即可

目前只知道根节点,遍历一下O(n)得出结论,但是要好于O(n),考虑完全二叉树特点 ?

空节点都在最后一层的右边,从根节点一直访问右孩子,知道访问到叶子节点,这时可能访问到最后一层或倒数第二层

失误

分析2.0

class Solution {
/**
* 针对完全二叉树的解法
*
* 满二叉树的结点数为:2^depth - 1
*/
public int countNodes(TreeNode root) {
if (root == null) return 0;
TreeNode left = root.left;
TreeNode right = root.right;
int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便
while (left != null) { // 求左子树深度
left = left.left;
leftDepth++;
}
while (right != null) { // 求右子树深度
right = right.right;
rightDepth++;
}
if (leftDepth == rightDepth) {
return (2 << leftDepth) - 1; // 注意(2<<1) 相当于2^2,所以leftDepth初始为0
}
return countNodes(root.left) + countNodes(root.right) + 1;
}
}

分析3.0

普通二叉树

class Solution {
// 通用递归解法
public int countNodes(TreeNode root) {
if(root == null) {
return 0;
}
return countNodes(root.left) + countNodes(root.right) + 1;
}
}

总结

  1. 使用前序求的就是深度,使用后序求的是高度
  2. 求最小值结果变量要初始化为数据集的最大值,求最大值要初始化为数据集的最小值
  3. 二叉树有一个很好的结构特点,某个操作可以平等地施加于所有节点,这样递归就特别方便,要求什么先求它的孩子
  4. 判断一颗完全二叉树是不是满二叉树向左右两边遍历

常用变量名增量更新

size、val、ans、cnt、cur、pre、next、left、right、index、gap、tar、res、src、len、start、end、flag、ch

代码随想录算法训练营day16 | leetcode ● 104.二叉树的最大深度 559.n叉树的最大深度 ● 111.二叉树的最小深度 ● 222.完全二叉树的节点个数的更多相关文章

  1. Java实现 LeetCode 222 完全二叉树的节点个数

    222. 完全二叉树的节点个数 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集 ...

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

    完全二叉树的节点个数 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最 ...

  3. LeetCode 222. 完全二叉树的节点个数(Count Complete Tree Nodes)

    题目描述 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位 ...

  4. LeetCode 222.完全二叉树的节点个数(C++)

    给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置.若最底 ...

  5. Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree)

    Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree) 深度优先搜索的解题详细介绍,点击 给定一个 N 叉树,找到其最大深度 ...

  6. Leetcode:559. N叉树的最大深度

    Leetcode:559. N叉树的最大深度 Leetcode:559. N叉树的最大深度 Talk is cheap . Show me the code . /* // Definition fo ...

  7. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  8. [LeetCode] Maximum Depth of N-ary Tree N叉树的最大深度

    Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...

  9. [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...

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

    题目 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置. ...

随机推荐

  1. Excel2010表格内容被加密,无法编辑内容。

    Sub PasswordBreaker() Dim i As Integer, j As Integer, k As Integer Dim l As Integer, m As Integer, n ...

  2. go-carbon 1.5.1 版本发布, 修复已知 bug 和新增土耳其翻译文件

    carbon 是一个轻量级.语义化.对开发者友好的golang时间处理库,支持链式调用. 目前已被 awesome-go 收录,如果您觉得不错,请给个star吧 github.com/golang-m ...

  3. 输出图形字符的命令 banner

    输出图形字符的命令 banner 有趣的 Linux 命令.来自实验楼操作系统课程 安装 sudo apt install sysvbanner 截图 其他 还有两个类似的命令toilet,figle ...

  4. 连表操作join 子查询 SQL补充 数据库软件navicat pymysql模块

    目录 多表查询的两种方法 方式1:连表操作 方式2:子查询 SQL补充知识点 1.分组之前字段拼接 concat concat_ws 2.SQL执行判断条件 exists 3.表相关SQL补充 修改表 ...

  5. 安装es可视化软件Kibana

    一 Kibana介绍 Kibana 是一款开源的数据分析和可视化平台,它是 Elastic Stack 成员之一,设计用于和 Elasticsearch 协作. 您.可以使用 Kibana 对 Ela ...

  6. Python中open()文件操作/OS目录操作

    File对象测试数据的读写与操作 #def open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, ...

  7. js将数组内属性值相同的项合并成二维数组

    var ary=[ {"RaDate":'2021-09-08',"Type":'Morning1','title':'测试1'}, {"RaDate ...

  8. 工业数据分析为什么要用FusionInsight MRS IoTDB?

    摘要:MRS IoTDB,它是华为FusionInsight MRS大数据套件中的时序数据库产品,在深度参与Apache IoTDB社区开源版的基础上推出的高性能企业级时序数据库产品. 本文分享自华为 ...

  9. python之路47 django路由层配置 虚拟环境

    可视化界面之数据增删改查 针对数据对象主键字段的获取可以使用更加方便的obj.pk获取 在模型类中定义双下str方法可以在数据对象被执行打印操作的时候方便的查看 ''' form表单中能够触发提交动作 ...

  10. SSM框架——SpringMVC

    SpringMVC MVC三层架构 Controller层:取得前端数据.调用相关业务逻辑.转发/重定向到其他页面 Model层:实现业务逻辑.保存数据 View层:显示页面 1.第一个MVC程序 新 ...