二叉树最小深度,最大深度,完全二叉树结点数

第六章 二叉树part03

今日内容:

  1. 104.二叉树的最大深度 559.n叉树的最大深度
  1. 111.二叉树的最小深度
  1. 222.完全二叉树的节点个数

详细布置

104.二叉树的最大深度 (优先掌握递归)

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例: 给定二叉树 [3,9,20,null,null,15,7],

返回它的最大深度 3 。

递归法

本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。

  • 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
  • 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始)

根节点的高度就是二叉树的最大深度 ,所以本题中我们通过后序求的根节点高度来求的二叉树最大深度。

后序遍历(左右中)来计算树的高度

  1. 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回这棵树的深度,所以返回值为int类型。
  2. 确定终止条件:如果为空节点的话,就返回0,表示高度为0。
  3. 确定单层递归的逻辑:先求它的左子树的深度,再求右子树的深度,最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。

递归法:

/**

* Definition for a binary tree node.

* public class TreeNode {

* int val;

* TreeNode left;

* TreeNode right;

* TreeNode() {}

* TreeNode(int val) { this.val = val; }

* TreeNode(int val, TreeNode left, TreeNode right) {

* this.val = val;

* this.left = left;

* this.right = right;

* }

* }

*/

class Solution {

public int maxDepth(TreeNode root) {

if(root==null) return 0;

int leftVal = maxDepth(root.left);

int rightVal = maxDepth(root.right);

return Math.max(leftVal,rightVal)+1;

}

}

回溯法:

/**

* Definition for a binary tree node.

* public class TreeNode {

* int val;

* TreeNode left;

* TreeNode right;

* TreeNode() {}

* TreeNode(int val) { this.val = val; }

* TreeNode(int val, TreeNode left, TreeNode right) {

* this.val = val;

* this.left = left;

* this.right = right;

* }

* }

*/

class Solution {

public int result;

public int maxDepth(TreeNode root) {

if(root==null) return result;

result = 0;

getMax(root,1);

return result;

}

public void getMax(TreeNode root,int depth)

{

result = result>depth?result:depth;

if(root.left==null&&root.right==null) return;

if(root.left!=null)

{

depth++;

getMax(root.left,depth);

depth--;

}

if(root.right!=null)

{

depth++;

getMax(root.right,depth);

depth--;

}

}

}

迭代法(BFS)

/**

* Definition for a binary tree node.

* public class TreeNode {

* int val;

* TreeNode left;

* TreeNode right;

* TreeNode() {}

* TreeNode(int val) { this.val = val; }

* TreeNode(int val, TreeNode left, TreeNode right) {

* this.val = val;

* this.left = left;

* this.right = right;

* }

* }

*/

class Solution {

public int maxDepth(TreeNode root) {

if(root==null) return 0;

Queue<TreeNode> queue = new LinkedList<>();

queue.offer(root);

int depth=0;

while(!queue.isEmpty())

{

depth++;

int size = queue.size();

for(int i=0;i<size;i++)

{

TreeNode cur = queue.poll();

if(cur.left!=null) queue.offer(cur.left);

if(cur.right!=null) queue.offer(cur.right);

}

}

return depth;

}

}

559.n叉树的最大深度

给定一个 n 叉树,找到其最大深度。

最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。

例如,给定一个 3叉树 :

我们应返回其最大深度,3。

递归法

/*

// Definition for a Node.

class Node {

public int val;

public List<Node> children;

public Node() {}

public Node(int _val) {

val = _val;

}

public Node(int _val, List<Node> _children) {

val = _val;

children = _children;

}

};

*/

class Solution {

public int maxDepth(Node root) {

if(root==null) return 0;

int depth = 0;

if(root.children!=null)

for(Node child:root.children) depth = Math.max(depth,maxDepth(child));

return depth+1;

}

}

迭代法(BFS)

/*

// Definition for a Node.

class Node {

public int val;

public List<Node> children;

public Node() {}

public Node(int _val) {

val = _val;

}

public Node(int _val, List<Node> _children) {

val = _val;

children = _children;

}

};

*/

class Solution {

public int maxDepth(Node root) {

if(root==null) return 0;

int depth = 0;

Queue<Node> queue = new LinkedList<>();

queue.offer(root);

while(!queue.isEmpty())

{

depth++;

int size = queue.size();

while(size>0)

{

Node cur = queue.poll();

for(int i=0;i<cur.children.size();i++)

{

if(cur.children.get(i)!=null)

queue.offer(cur.children.get(i));

}

size--;

}

}

return depth;

}

}

111.二叉树的最小深度 (优先掌握递归)

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [3,9,20,null,null,15,7],

返回它的最小深度 2

  1. 确定递归函数的参数和返回值

参数为要传入的二叉树根节点,返回的是int类型的深度。

  1. 确定终止条件

终止条件也是遇到空节点返回0,表示当前节点的高度为0。

  1. 确定单层递归的逻辑

如果左子树为空,右子树不为空,说明最小深度是 1 + 右子树的深度。

反之,右子树为空,左子树不为空,最小深度是 1 + 左子树的深度。 最后如果左右子树都不为空,返回左右子树深度最小值 + 1 。

遍历的顺序为后序(左右中),可以看出:求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑。

/**

* Definition for a binary tree node.

* public class TreeNode {

* int val;

* TreeNode left;

* TreeNode right;

* TreeNode() {}

* TreeNode(int val) { this.val = val; }

* TreeNode(int val, TreeNode left, TreeNode right) {

* this.val = val;

* this.left = left;

* this.right = right;

* }

* }

*/

class Solution {

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;

return Math.min(leftDepth,rightDepth)+1;

}

}

222.完全二叉树的节点个数(优先掌握递归)

给出一个完全二叉树,求出该树的节点个数。

示例 1:

  • 输入:root = [1,2,3,4,5,6]
  • 输出:6

示例 2:

  • 输入:root = []
  • 输出:0

示例 3:

  • 输入:root = [1]
  • 输出:1

提示:

  • 树中节点的数目范围是[0, 5 * 10^4]
  • 0 <= Node.val <= 5 * 10^4
  • 题目数据保证输入的树是 完全二叉树

普通二叉树

首先按照普通二叉树的逻辑来求。

递归法:

  1. 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回以该节点为根节点二叉树的节点数量,所以返回值为int类型。
  2. 确定终止条件:如果为空节点的话,就返回0,表示节点数为0。
  3. 确定单层递归的逻辑:先求它的左子树的节点数量,再求右子树的节点数量,最后取总和再加一 (加1是因为算上当前中间节点)就是目前节点为根节点的节点数量。
  4. /**
  5. * Definition for a binary tree node.
  6. * public class TreeNode {
  7. * int val;
  8. * TreeNode left;
  9. * TreeNode right;
  10. * TreeNode() {}
  11. * TreeNode(int val) { this.val = val; }
  12. * TreeNode(int val, TreeNode left, TreeNode right) {
  13. * this.val = val;
  14. * this.left = left;
  15. * this.right = right;
  16. * }
  17. * }
  18. */
  19. class Solution {
  20. public int countNodes(TreeNode root) {
  21. if(root==null) return 0;
  22. int leftNum = countNodes(root.left);
  23. int rightNum = countNodes(root.right);
  24. return leftNum+rightNum+1;
  25. }
  26. }

迭代法:

/**

* Definition for a binary tree node.

* public class TreeNode {

* int val;

* TreeNode left;

* TreeNode right;

* TreeNode() {}

* TreeNode(int val) { this.val = val; }

* TreeNode(int val, TreeNode left, TreeNode right) {

* this.val = val;

* this.left = left;

* this.right = right;

* }

* }

*/

class Solution {

public int countNodes(TreeNode root) {

if(root==null) return 0;

Queue<TreeNode> queue = new LinkedList<>();

int nodeNum = 0;

queue.offer(root);

while(!queue.isEmpty())

{

TreeNode cur = queue.poll();

nodeNum++;

if(cur.left!=null) queue.offer(cur.left);

if(cur.right!=null) queue.offer(cur.right);

}

return nodeNum;

}

}

完全二叉树

在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2^(h-1) 个节点。

完全二叉树只有两种情况,情况一:就是满二叉树,情况二:最后一层叶子节点没有满。

对于情况一,可以直接用 2^树深度 - 1 来计算,注意这里根节点深度为1。

对于情况二,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。

判断其子树是不是满二叉树,如果是则利用公式计算这个子树(满二叉树)的节点数量,如果不是则继续递归。

/**

* Definition for a binary tree node.

* public class TreeNode {

* int val;

* TreeNode left;

* TreeNode right;

* TreeNode() {}

* TreeNode(int val) { this.val = val; }

* TreeNode(int val, TreeNode left, TreeNode right) {

* this.val = val;

* this.left = left;

* this.right = right;

* }

* }

*/

class Solution {

public int countNodes(TreeNode root) {

if(root==null) return 0;

TreeNode left = root.left;

TreeNode right =root.right;

int leftDepth = 0;

int rightDepth = 0;

while(left!=null)

{

left = left.left;

leftDepth++;

}

while(right!=null)

{

right = right.right;

rightDepth++;

}

if(leftDepth==rightDepth) return (2<<leftDepth)-1;

int leftNum = countNodes(root.left);

int rightNum = countNodes(root.right);

return leftNum+rightNum+1;

}

}

第六章 二叉树part03

今日内容:

  1. 104.二叉树的最大深度 559.n叉树的最大深度
  1. 111.二叉树的最小深度
  1. 222.完全二叉树的节点个数

详细布置

104.二叉树的最大深度 (优先掌握递归)

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例: 给定二叉树 [3,9,20,null,null,15,7],

返回它的最大深度 3 。

递归法

本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。

  • 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
  • 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始)

根节点的高度就是二叉树的最大深度 ,所以本题中我们通过后序求的根节点高度来求的二叉树最大深度。

后序遍历(左右中)来计算树的高度

  1. 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回这棵树的深度,所以返回值为int类型。
  2. 确定终止条件:如果为空节点的话,就返回0,表示高度为0。
  3. 确定单层递归的逻辑:先求它的左子树的深度,再求右子树的深度,最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。

递归法:

/**

* Definition for a binary tree node.

* public class TreeNode {

* int val;

* TreeNode left;

* TreeNode right;

* TreeNode() {}

* TreeNode(int val) { this.val = val; }

* TreeNode(int val, TreeNode left, TreeNode right) {

* this.val = val;

* this.left = left;

* this.right = right;

* }

* }

*/

class Solution {

public int maxDepth(TreeNode root) {

if(root==null) return 0;

int leftVal = maxDepth(root.left);

int rightVal = maxDepth(root.right);

return Math.max(leftVal,rightVal)+1;

}

}

回溯法:

/**

* Definition for a binary tree node.

* public class TreeNode {

* int val;

* TreeNode left;

* TreeNode right;

* TreeNode() {}

* TreeNode(int val) { this.val = val; }

* TreeNode(int val, TreeNode left, TreeNode right) {

* this.val = val;

* this.left = left;

* this.right = right;

* }

* }

*/

class Solution {

public int result;

public int maxDepth(TreeNode root) {

if(root==null) return result;

result = 0;

getMax(root,1);

return result;

}

public void getMax(TreeNode root,int depth)

{

result = result>depth?result:depth;

if(root.left==null&&root.right==null) return;

if(root.left!=null)

{

depth++;

getMax(root.left,depth);

depth--;

}

if(root.right!=null)

{

depth++;

getMax(root.right,depth);

depth--;

}

}

}

迭代法(BFS)

/**

* Definition for a binary tree node.

* public class TreeNode {

* int val;

* TreeNode left;

* TreeNode right;

* TreeNode() {}

* TreeNode(int val) { this.val = val; }

* TreeNode(int val, TreeNode left, TreeNode right) {

* this.val = val;

* this.left = left;

* this.right = right;

* }

* }

*/

class Solution {

public int maxDepth(TreeNode root) {

if(root==null) return 0;

Queue<TreeNode> queue = new LinkedList<>();

queue.offer(root);

int depth=0;

while(!queue.isEmpty())

{

depth++;

int size = queue.size();

for(int i=0;i<size;i++)

{

TreeNode cur = queue.poll();

if(cur.left!=null) queue.offer(cur.left);

if(cur.right!=null) queue.offer(cur.right);

}

}

return depth;

}

}

559.n叉树的最大深度

给定一个 n 叉树,找到其最大深度。

最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。

例如,给定一个 3叉树 :

递归法

/*

// Definition for a Node.

class Node {

public int val;

public List<Node> children;

public Node() {}

public Node(int _val) {

val = _val;

}

public Node(int _val, List<Node> _children) {

val = _val;

children = _children;

}

};

*/

class Solution {

public int maxDepth(Node root) {

if(root==null) return 0;

int depth = 0;

if(root.children!=null)

for(Node child:root.children) depth = Math.max(depth,maxDepth(child));

return depth+1;

}

}

迭代法(BFS)

/*

// Definition for a Node.

class Node {

public int val;

public List<Node> children;

public Node() {}

public Node(int _val) {

val = _val;

}

public Node(int _val, List<Node> _children) {

val = _val;

children = _children;

}

};

*/

class Solution {

public int maxDepth(Node root) {

if(root==null) return 0;

int depth = 0;

Queue<Node> queue = new LinkedList<>();

queue.offer(root);

while(!queue.isEmpty())

{

depth++;

int size = queue.size();

while(size>0)

{

Node cur = queue.poll();

for(int i=0;i<cur.children.size();i++)

{

if(cur.children.get(i)!=null)

queue.offer(cur.children.get(i));

}

size--;

}

}

return depth;

}

}

111.二叉树的最小深度 (优先掌握递归)

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [3,9,20,null,null,15,7],

返回它的最小深度 2

  1. 确定递归函数的参数和返回值

参数为要传入的二叉树根节点,返回的是int类型的深度。

  1. 确定终止条件

终止条件也是遇到空节点返回0,表示当前节点的高度为0。

  1. 确定单层递归的逻辑

如果左子树为空,右子树不为空,说明最小深度是 1 + 右子树的深度。

反之,右子树为空,左子树不为空,最小深度是 1 + 左子树的深度。 最后如果左右子树都不为空,返回左右子树深度最小值 + 1 。

遍历的顺序为后序(左右中),可以看出:求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑。

/**

* Definition for a binary tree node.

* public class TreeNode {

* int val;

* TreeNode left;

* TreeNode right;

* TreeNode() {}

* TreeNode(int val) { this.val = val; }

* TreeNode(int val, TreeNode left, TreeNode right) {

* this.val = val;

* this.left = left;

* this.right = right;

* }

* }

*/

class Solution {

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;

return Math.min(leftDepth,rightDepth)+1;

}

}

222.完全二叉树的节点个数(优先掌握递归)

给出一个完全二叉树,求出该树的节点个数。

示例 1:

  • 输入:root = [1,2,3,4,5,6]
  • 输出:6

示例 2:

  • 输入:root = []
  • 输出:0

示例 3:

  • 输入:root = [1]
  • 输出:1

提示:

  • 树中节点的数目范围是[0, 5 * 10^4]
  • 0 <= Node.val <= 5 * 10^4
  • 题目数据保证输入的树是 完全二叉树

普通二叉树

首先按照普通二叉树的逻辑来求。

递归法:

  1. 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回以该节点为根节点二叉树的节点数量,所以返回值为int类型。
  2. 确定终止条件:如果为空节点的话,就返回0,表示节点数为0。
  3. 确定单层递归的逻辑:先求它的左子树的节点数量,再求右子树的节点数量,最后取总和再加一 (加1是因为算上当前中间节点)就是目前节点为根节点的节点数量。
  4. /**
  5. * Definition for a binary tree node.
  6. * public class TreeNode {
  7. * int val;
  8. * TreeNode left;
  9. * TreeNode right;
  10. * TreeNode() {}
  11. * TreeNode(int val) { this.val = val; }
  12. * TreeNode(int val, TreeNode left, TreeNode right) {
  13. * this.val = val;
  14. * this.left = left;
  15. * this.right = right;
  16. * }
  17. * }
  18. */
  19. class Solution {
  20. public int countNodes(TreeNode root) {
  21. if(root==null) return 0;
  22. int leftNum = countNodes(root.left);
  23. int rightNum = countNodes(root.right);
  24. return leftNum+rightNum+1;
  25. }
  26. }

迭代法:

/**

* Definition for a binary tree node.

* public class TreeNode {

* int val;

* TreeNode left;

* TreeNode right;

* TreeNode() {}

* TreeNode(int val) { this.val = val; }

* TreeNode(int val, TreeNode left, TreeNode right) {

* this.val = val;

* this.left = left;

* this.right = right;

* }

* }

*/

class Solution {

public int countNodes(TreeNode root) {

if(root==null) return 0;

Queue<TreeNode> queue = new LinkedList<>();

int nodeNum = 0;

queue.offer(root);

while(!queue.isEmpty())

{

TreeNode cur = queue.poll();

nodeNum++;

if(cur.left!=null) queue.offer(cur.left);

if(cur.right!=null) queue.offer(cur.right);

}

return nodeNum;

}

}

完全二叉树

在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2^(h-1) 个节点。

完全二叉树只有两种情况,情况一:就是满二叉树,情况二:最后一层叶子节点没有满。

对于情况一,可以直接用 2^树深度 - 1 来计算,注意这里根节点深度为1。

对于情况二,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。

判断其子树是不是满二叉树,如果是则利用公式计算这个子树(满二叉树)的节点数量,如果不是则继续递归。

/**

* Definition for a binary tree node.

* public class TreeNode {

* int val;

* TreeNode left;

* TreeNode right;

* TreeNode() {}

* TreeNode(int val) { this.val = val; }

* TreeNode(int val, TreeNode left, TreeNode right) {

* this.val = val;

* this.left = left;

* this.right = right;

* }

* }

*/

class Solution {

public int countNodes(TreeNode root) {

if(root==null) return 0;

TreeNode left = root.left;

TreeNode right =root.right;

int leftDepth = 0;

int rightDepth = 0;

while(left!=null)

{

left = left.left;

leftDepth++;

}

while(right!=null)

{

right = right.right;

rightDepth++;

}

if(leftDepth==rightDepth) return (2<<leftDepth)-1;

int leftNum = countNodes(root.left);

int rightNum = countNodes(root.right);

return leftNum+rightNum+1;

}

}

相关推荐
吱吱鼠叔几秒前
MATLAB计算与建模常见函数:5.曲线拟合
算法·机器学习·matlab
嵌入式AI的盲1 小时前
数组指针和指针数组
数据结构·算法
reyas3 小时前
B树系列解析
数据结构·b树
Indigo_code4 小时前
【数据结构】【顺序表算法】 删除特定值
数据结构·算法
__AtYou__4 小时前
Golang | Leetcode Golang题解之第448题找到所有数组中消失的数字
leetcode·golang·题解
阿史大杯茶4 小时前
Codeforces Round 976 (Div. 2 ABCDE题)视频讲解
数据结构·c++·算法
LluckyYH5 小时前
代码随想录Day 58|拓扑排序、dijkstra算法精讲,题目:软件构建、参加科学大会
算法·深度优先·动态规划·软件构建·图论·dfs
转调5 小时前
每日一练:地下城游戏
开发语言·c++·算法·leetcode
不穿格子衬衫5 小时前
常用排序算法(下)
c语言·开发语言·数据结构·算法·排序算法·八大排序
wdxylb5 小时前
使用C++的OpenSSL 库实现 AES 加密和解密文件
开发语言·c++·算法