二叉树和完全二叉树的深度及节点个数计算
介绍 二叉树是一种非常重要的数据结构,在计算机科学中广泛应用,它的一些特性使得它可以在搜索、排序、树形结构等领域得到广泛应用。而完全二叉树是一种特殊的二叉树,它的每一层都被节点填满,除了最后一层,最后一层的节点从左到右依次填充。
在本文中,我们将介绍如何计算二叉树和完全二叉树的深度以及节点个数。我们将使用Java代码来实现这些算法,其中二叉树的节点类为:
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
完全二叉树的节点类为:
class CBTNode {
int val;
CBTNode left;
CBTNode right;
CBTNode(int x) { val = x; }
}
二叉树的最大深度 题目描述:给定一个二叉树,找出其最大深度。
算法:递归
实现思路:二叉树的深度等于左子树和右子树深度的最大值加1。
Java代码实现:
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;
}
}
二叉树的最小深度 题目描述:给定一个二叉树,找出其最小深度。
算法:递归
实现思路:如果当前节点为空,返回0;如果当前节点为叶子节点,返回1;如果当前节点只有左子树或者只有右子树,返回左子树或者右子树的深度加1;否则返回左子树和右子树深度的最小值加1。
Java代码实现:
class Solution {
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
if (root.left == null) {
return minDepth(root.right) + 1;
}
if (root.right == null) {
return minDepth(root.left) + 1;
}
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
}
}
完全二叉树的节点个数 题目描述:给定一个完全二叉树,求出该树的节点个数。
算法:递归
实现思路:完全二叉树的节点数可以分为两部分:左子树和右子树。如果左子树的深度等于右子树的深度,说明左子树是一棵满二叉树,可以直接计算出左子树的节点个数,然后递归计算右子树;如果左子树的深度大于右子树的深度,说明右子树是一棵满二叉树,可以直接计算出右子树的节点个数,然后递归计算左子树。
Java代码实现:
class Solution {
public int countNodes(CBTNode root) {
if (root == null) {
return 0;
}
int leftDepth = getDepth(root.left);
int rightDepth = getDepth(root.right);
if (leftDepth == rightDepth) {
return (1 << leftDepth) + countNodes(root.right);
} else {
return (1 << rightDepth) + countNodes(root.left);
}
}
private int getDepth(CBTNode root) {
int depth = 0;
while (root != null) {
depth++;
root = root.left;
}
return depth;
}
}
完整代码
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
class CBTNode {
int val;
CBTNode left;
CBTNode right;
CBTNode(int x) { val = x; }
}
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;
}
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
if (root.left == null) {
return minDepth(root.right) + 1;
}
if (root.right == null) {
return minDepth(root.left) + 1;
}
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
}
public int countNodes(CBTNode root) {
if (root == null) {
return 0;
}
int leftDepth = getDepth(root.left);
int rightDepth = getDepth(root.right);
if (leftDepth == rightDepth) {
return (1 << leftDepth) + countNodes(root.right);
} else {
return (1 << rightDepth) + countNodes(root.left);
}
}
private int getDepth(CBTNode root) {
int depth = 0;
while (root != null) {
depth++;
root = root.left;
}
return depth;
}
}
总结 本文介绍了如何计算二叉树和完全二叉树的深度以及节点个数,其中二叉树的深度和最小深度使用递归算法实现,完全二叉树的节点个数使用递归算法和位运算实现。这些算法不仅适用于计算二叉树和完全二叉树的深度和节点个数,也可以应用于其他相关的问题,如平衡二叉树、判断一棵树是否为二叉搜索树等。