代码随想录刷题Day20

85 阅读3分钟
  1. 104. 二叉树的最大深度
  • 迭代(BFS)方法已经在Day18的第9题用过,今天主要用的是递归(DFS)
  • (1)后序遍历

本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。

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

image.png

而根节点的高度就是二叉树的最大深度,所以本题中我们通过后序求的根节点高度来求的二叉树最大深度。

class Solution {
    public int maxDepth(TreeNode root) {
        return getmaxDepth(root);
    }
    public int getmaxDepth(TreeNode root){
        //终止条件:如果为空节点的话,就返回0,表示高度为0。
        if(root==null){
            return 0;
        }
      //先求它的左子树的深度,再求右子树的深度,最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。
        int depth=Math.max(getmaxDepth(root.left) , getmaxDepth(root.right))+1;
        return depth;
    }
}
  • (2)前序遍历

前序与后序的区别就在于信息的采集:

  • 前序:先进行处理,再遍历左右子树,即用父亲信息处理孩子
  • 后序:先遍历左右子树,再进行处理,即用孩子信息处理父亲
    public int maxDepth(TreeNode root) {
        return getmaxDepth(root,0);
    }
    public int getmaxDepth(TreeNode root,int depth){
        if(root==null){
            return 0;
        }
        //进入该节点,深度+1
        depth++;
        //该节点没有孩子时,返回深度
        if(root.left == null && root.right == null)
            return depth;
        //有孩子就继续递归,比较左右深度
        return Math.max(getmaxDepth(root.left,depth),getmaxDepth(root.right,depth));
    }
}
  1. 559. N 叉树的最大深度
  • 递归
class Solution {
    public int maxDepth(Node root) {
        if(root==null){
            return 0;
        }
        int depth=0;
        for(Node child:root.children){
            depth=Math.max(depth,maxDepth(child));
        }
        return depth+1;
    }
}
  • 迭代
    public int maxDepth(Node root) {
        int maxDepth=0;
        Queue<Node> queue = new ArrayDeque<>();
        if(root!=null){
            queue.add(root);
        }
        // while 循环的每一轮中,都是将当前层的所有结点出队列,再将下一层的所有结点入队列
        while(!queue.isEmpty()){
            int n=queue.size();//记录队列中的节点数量n
            for(int i=0;i<n;i++){// 变量 i 无实际意义,只是为了循环 n 次
                Node node = queue.poll();
                //遍历当前节点的孩子,加入队列
                for(Node child:node.children){
                    queue.add(child);
                }
            }
            maxDepth++;
        }
        return maxDepth;
    }
}
  1. 111. 二叉树的最小深度
  • 迭代(BFS)方法已经在Day18的第10题用过,今天主要用的是递归(DFS)
  • (1)后序遍历
    public int minDepth(TreeNode root) {
        if (root ==null) return 0;
        if(root.left == null && root.right == null) return 1;
        int m1 = minDepth(root.left);
        int m2 = minDepth(root.right);
        //碰到右子树为空的情况,返回的是左子树的最小深度
        if (root.left != null && root.right == null) {
            return 1+m1;
        }
        //碰到左子树为空的情况,返回的是右子树的最小深度
        if (root.left == null && root.right != null) {
            return 1+m2;
        }
        //左右子树都不为空的情况呢,比较左右子树的最小深度
        return Math.min(m1,m2) + 1; 
    }
}
  • (2)前序遍历
    public int minDepth(TreeNode root) {
        return getminDepth(root,0);
    }
    public int getminDepth(TreeNode root,int depth){
        if(root==null){
            return 0;
        }
        //进入该节点,深度+1
        depth++;
        //该节点没有孩子时,返回深度
        if(root.left == null && root.right == null)
            return depth;
        if (root.left != null && root.right == null)
            return getminDepth(root.left,depth);
        if (root.left == null && root.right != null)
            return getminDepth(root.right,depth);
        //有孩子就继续递归,比较左右深度
        return Math.min(getminDepth(root.left,depth),getminDepth(root.right,depth));
    }
}
  1. 222. 完全二叉树的节点个数
  • 递归
    public int countNodes(TreeNode root) {
        if(root==null) return 0;
        return countNodes(root.left)+countNodes(root.right)+1;
    }
}
  • 迭代
class Solution {
    public int countNodes(TreeNode root) {
        Queue<TreeNode> queue= new ArrayDeque<>();
        if(root!=null){
            queue.add(root);
        }
        int count=0;
        while(!queue.isEmpty()){
            int n=queue.size();
            for(int i=0;i<n;i++){
                TreeNode node=queue.poll();
                count++;
                if(node.left!=null){
                    queue.add(node.left);
                }
                if(node.right!=null){
                    queue.add(node.right);
                }
            }
        }
        return count;
    }
}