【代码随想录|刷题记录Day16】104.二叉树的最大深度、505.n叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数

45 阅读4分钟

题目列表

  104.二叉树的最大深度

  505.n叉树的最大深度

  111.二叉树的最小深度

  222.完全二叉树的节点个数

解题过程

1、104.二叉树的最大深度

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

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

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

思路: 本题可以用前序遍历求深度,也可以用后序遍历求高度。

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

易知,根节点的高度就是二叉树的最大深度。

递归法

  • 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回这棵树的深度,所以返回值为int类型。
  • 确定终止条件:如果为空节点的话,就返回0,表示高度为0。
  • 确定单层递归的逻辑:先求它的左子树的深度,再求右子树的深度,最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。
//简洁版
class Solution {
    public int maxDepth(TreeNode root) {
        //终止条件
        if (root == null) {
            return 0;
        }
        int leftDepth = maxDepth(root.left);
        int rightDepth = maxDepth(root.right);
        return 1 + Math.max(leftDepth, rightDepth);
    }
}
//递归求深度法(回溯特性)
class Solution {
    //最大深度
    public int maxnum = 0;

    public int maxDepth(TreeNode root) {
        if (root != null) {
            depthReverse(root, 1);
        }
        return maxnum;
    }

    public void depthReverse(TreeNode node, int depth) {
        if (node == null) {
            return;
        }
        maxnum = maxnum > depth ? maxnum : depth;
        depthReverse(node.left, depth + 1);
        depthReverse(node.right, depth + 1);
    }
}

迭代法: 在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度。

class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
         //最大深度
        int maxnum = 0;
        while (!queue.isEmpty()) {
            int len = queue.size();
            maxnum++;
            while (len-- > 0) {
                TreeNode node = queue.poll();
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }    
            }
        }
        return maxnum;
    }
}

2、505.n叉树的最大深度

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

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

N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔。

递归法

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;
    }
}

迭代法

class Solution {
    public int maxDepth(Node root) {
        if (root == null) {
            return 0;
        }
        Queue<Node> queue = new LinkedList<Node>();
        queue.offer(root);
         //最大深度
        int depth = 0;
        while (!queue.isEmpty()) {
            int len = queue.size();
            depth++;
            while (len-- > 0) {
                Node node = queue.poll();
                if (node.children != null) {
                    for (Node child : node.children) {
                        queue.offer(child);
                    }
                }
            }
        }
        return depth;
    }
}

3、111.二叉树的最小深度

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

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

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

思路: 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。叶子节点是左右孩子都为空的节点

递归法

  • 确定递归函数的参数和返回值
  • 确定终止条件
  • 确定单层递归的逻辑
    • 如果左子树为空,右子树不为空,最小深度是 1 + 右子树的深度。
    • 如果右子树为空,左子树不为空,最小深度是 1 + 左子树的深度。
    • 如果左右子树都不为空,返回左右子树深度最小值 + 1 。
class Solution {
    public int minDepth(TreeNode root) {
        //终止条件
        if (root == null) {
            return 0;
        }
        int leftDepth = minDepth(root.left); 
        int rightDepth = minDepth(root.right); 
        if (root.right == null) {
            return 1 + leftDepth;
        } 
        if (root.left == null) {
            return 1 + rightDepth;
        } 
        return 1 + Math.min(leftDepth, rightDepth);
    }
}

迭代法

class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
         //最大深度
        int depth = 0;
        while (!queue.isEmpty()) {
            int len = queue.size();
            depth++;
            while (len-- > 0) {
                TreeNode node = queue.poll();
                if (node.left == null && node.right == null) {
                    return depth;
                }
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
        }
        return depth;
    }
}

注意

求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑。

4、222.完全二叉树的节点个数

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

递归法

class Solution {
    public int countNodes(TreeNode root) {
        //终止条件
        if (root == null) {
            return 0;
        }
        int leftDepth = countNodes(root.left);
        int rightDepth = countNodes(root.right);
        return 1 + leftDepth + rightDepth;
    }
}

迭代法

class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
         //节点个数
        int nodeSum = 0;
        while (!queue.isEmpty()) {
            int len = queue.size();
            while (len-- > 0) {
                nodeSum++;
                TreeNode node = queue.poll();
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
        }
        return nodeSum;
    }
}

总结

今天拔了颗智齿,有点难受,差点开摆了。今天的几道题只要知道思路就很好写,需要注意一下树的深度和高度定义的区别和联系。