基础知识

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

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. Android ViewPager2 + Fragment + BottomNavigationView 联动

    Android ViewPager2 + Fragment + BottomNavigationView 联动 本篇主要介绍一下 ViewPager2 + Fragment + BottomNavig ...

  2. Shell及Linux常见易错题目题库-Shell/Linux-选择、简答、判断、编程

    1.以下不合法的shell头是(不合法指运行会报错)(   ) A. #!/bin/bash B. #-/bin/bash C. !#/bin/bash 答案:C 2.if [ $2 -a $2 = ...

  3. Velero 系列文章(一):基础

    概述 Velero 是一个开源工具,可以安全地备份和还原,执行灾难恢复以及迁移 Kubernetes 集群资源和持久卷. 灾难恢复 Velero 可以在基础架构丢失,数据损坏和/或服务中断的情况下,减 ...

  4. Burpsuite2022.1详细图文安装教程(含工具链接)

    ​ 应用概述: Burp Suite 是用于攻击web 应用程序的集成平台,包含了许多工具.Burp Suite为这些工具设计了许多接口,以加快攻击应用程序的过程.所有工具都共享一个请求,并能处理对应 ...

  5. 数据库连接池的一些基本理解,c3p0和druid

    数据库连接池 1,概念: 其实就是一个容器(集合),存放数据库连接的容器. 当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户来采访数据时,从容器中获取连接对象,用户访问完后,会将连接对象 ...

  6. day02-Promise

    Promise 1.Promise基本介绍 Promise是异步编程的一种解决方案,可以解决传统Ajax回调函数嵌套问题. 传统的Ajax异步调用在需要多个操作的时候,会导致多个回调函数嵌套,导致代码 ...

  7. Python网络爬虫get方法出现乱码的解决的三种方案

    给大家祭出网络爬虫过程中三种中文乱码的处理方案,希望对大家的学习有所帮助. 方案一 将requests.get().text改为requests.get().content 我们可以看到通过text( ...

  8. 《STL源码剖析》traits技法分析

    在完成一个迭代器的时候,我们可能会暴露太多的细节在外面,为了将这些细节给隐藏,我们需要封装,这也是为什么每一种STL容器都提供了一种专属的迭代器. 为了解决以"迭代器所指对象的型别" ...

  9. lwl-resume 个人简历编辑使用说明

    LWL RESUME 是一个简洁.可自定义生成pdf的在线简历编辑工具! 如果你喜欢这种简约格式的简历风格,可以尝试用它来编辑简历 . 注意事项 不支持手机端编辑!请务必使用最新版Chrome(谷歌浏 ...

  10. BUG日记之-----Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway.

    在使用swagger进行测试的时候调用弹窗  解决办法: 在启动类添加@EnableSwagger2注解