104.二叉树的最大深度
- 104.二叉树的最大深度 | 题目链接
- 代码随想录 | 讲解链接
- 题意:给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
- 思路:根节点的高度就是二叉树的最大深度,通过后序求的根节点高度来求的二叉树最大深度
- 确定递归函数的参数和返回值:参数就是传入根节点,返回就返回这棵树的深度,返回值类型为int。
- 确定终止条件:如果为空节点的话,就返回0,表示高度为0;
- 确定单层递归的逻辑:先求左子树深度,再求右子树深度,最后取左右深度最大的数值,再+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 leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
return Math.max(leftDepth, rightDepth) + 1;
}
}
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 1 + depth;
}
}
111.二叉树的最小深度
-
题意:最小深度是从根节点到最近的叶子节点的最短路径上的节点数量,叶子节点是左右孩子都为空的节点!
-
思路:递归三部曲
- 确定传入参数和返回值:传入根节点,返回最小的深度,类型为int
- 确定终止条件:如果遇到节点为null,就返回0,表示当前节点的高度为0;
- 确定单层递归逻辑
- 左子树为null,右子树不为空,最小深度为1+右子树深度
- 右子树为null,左子树不为空,最小深度是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 && root.right != null) {
return 1 + rightDepth;
} else if(root.left != null && root.right == null) {
return 1 + leftDepth;
}
int res = 1 + Math.min(leftDepth, rightDepth);
return res;
}
}
222. 完全二叉树的节点个数
- 222. 完全二叉树的节点个数 | 题目链接
- 代码随想录 | 讲解链接
- 题意:给出一个完全二叉树,求出该树的节点个数。
- 思路:递归。先把全二叉树的情况剪枝。
- 剪枝全二叉树:判断左右子树的深度是否相同,相同则为全二叉树,不相同则为完全二叉树,就走递归逻辑
- 判断终止条件,当节点为null是返回个数0;
- 递归逻辑:判断左子树的个数,右子树的个数,最后在加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;
}
//如果不是全二叉树,是完全二叉树,再走递归逻辑
return countNodes(root.left) + countNodes(root.right) + 1;
}
}