学习目标:
第六章 二叉树
-
104.二叉树的最大深度 559.n叉树的最大深度
-
111.二叉树的最小深度
-
222.完全二叉树的节点个数
学习内容:
**104.二叉树的最大深度 **
思路:递归法:采用后序遍历——左右中,通过递归找到叶子节点,随后通过后序遍历将该节点的深度告诉父节点,持续递归向上得到最大深度
//递归法:采用后序遍历——左右中,通过递归找到叶子节点,随后通过后序遍历将该节点的深度告诉父节点,持续递归向上得到最大深度
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叉树的最大深度
思路:递归法,后序遍历求root节点的高度,根据递归判断是否存在孩子来找到叶子节点从而实现自底向上自增一完成最大深度的运算
//n叉树的最大深度
/*递归法,后序遍历求root节点的高度,根据递归判断是否存在孩子来找到叶子节点从而实现自底向上自增一完成最大深度的运算*/
public int maxDepth01(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; //中节点
}
111.二叉树的最小深度
思路:依旧是使用后序遍历——左右中,通过递归找到叶子节点,与最大深度相反找到最小值返回即可,但需要注意根节点的左右子树是否为空的情况
/**
* 递归法,相比求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;
}
222.完全二叉树的节点个数
(1)递归法思路:老老实实遍历每一个节点
// 通用递归解法,遍历每一个节点
public int countNodes(TreeNode root) {
if(root == null) {
return 0;
}
return countNodes(root.left) + countNodes(root.right) + 1;
}
(2)针对完全二叉树的解法
思路:根据判断左右子树深度是否相等来判断是否为满二叉树,若是则可以根据公式直接计算也就是2的深度次方减一
/**
* 针对完全二叉树的解法
*
* 满二叉树的结点数为:2^depth - 1
*/
//思路:根据判断左右子树深度是否相等来判断是否为满二叉树,若是则可以根据公式直接计算也就是2的深度次方减一
public int countNodes01(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,也就是二进制中2 == 10,2<<1也就是变成4 == 100,所以leftDepth初始为0
}
return countNodes(root.left) + countNodes(root.right) + 1;//依旧是左右中后序遍历
}
学习时间:
下午五小时。