104.二叉树的最大深度
深度:任意一节点到根节点的距离
高度:任意一节点到叶子节点的距离
求高度:后序遍历(左右中),从下往上去计数,叶子节点将结果返回给父节点
父节点+1,就是父节点的高度。根节点的高度就是二叉树的最大深度。
求深度:前序遍历(中左右),往下遍历一个就+1
求根节点的高度:根节点的高度就是二叉树的深度
后序遍历:
左右中
int getHeight( node) {
if (node == null) return 0; // 高度为0
左:int leftHeight = getHeight(node.left);
右:int rightHeight = getHeight(node.right);
中:int height = 1 + max(leftHeight, rightHeight); //当前节点的父节点的高度
return height;
}
精简版:
return 1 + max(getHeight(node.left, getHeight(node.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叉树的最大深度
class Solution {
/*递归法,后序遍历求root节点的高度*/
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; //中节点
}
}
111.二叉树的最小深度
最小深度:根节点到最近的叶子节点的最小距离
如下图,最小深度为3
求根节点的最小高度得最小深度
int getHeight( TreeNode node) {
if(node == null) return 0;
左:int leftHeight = getHeight(node.left);
右:int rightHeight = getHeight(node.right);
if(node.left == null && node.right != null) return 1 + rightHeight;
if(node.left != null && node.right == null) return 1 + leftHeight;
//左右子树都不为空:
result = 1 + min(leftHeight, rightHeight);
return result;
题解
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;
if (root.left != null && root.right == null) return 1 + leftDepth;
return Math.min(leftDepth, rightDepth) + 1;
}
}
222.完全二叉树的节点个数
后序遍历:
普通二叉树:
int getNum(node) {
if(node == null) return 0;
leftNum = getNum(node.left);
rightNum = getNum(node.right);
result = leftNum + rightNum + 1;
return result;
精简:
return getNum(node.left) + getNum(node.right) + 1;
完全二叉树:
可以先判断是不是满二叉树,如果左右子树的深度相同,则是满二叉树,可以用
公式2的n次方-1,单个叶子节点一定是满二叉树
int getNum(node) {
if(node == null) return 0;
left = node.left;
right = node.right;
leftDepth = 0; //左侧深度
rightDepth = 0; //右侧深度
while (left) {
left = left.left;
leftDepth++;
}
while (right) {
right = right.right;
rightDepth ++;
}
if (leftDepth == rightDepth) return 2^leftDepth - 1;
leftNum = getNum(node.left);
rightNum = getNum(node.right);
result = leftNum + rightNum + 1;
return result;
题解
class Solution {
// 通用递归解法
public int countNodes(TreeNode root) {
if(root == null) {
return 0;
}
return countNodes(root.left) + countNodes(root.right) + 1;
}
}
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;
}
}