算法学习day16

104 二叉树的最大深度

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

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

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

示例:
给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7
返回它的最大深度 3 。
递归
  • 入参:根节点
  • 递归终止,节点为null, return 0 ;
  • 循环条件:当前层级1+左右子节点的最大深度
  • 返回最大深度
class Solution {
    int[] deep = new int[2];
    public int maxDepth(TreeNode root) {
        if (root == null) return 0 ;
        return 1+Math.max(maxDepth(root.left), maxDepth(root.right));
    }
   
}

迭代
  • 层序遍历的核心时一层一层遍历,可使用通用模板,累加层级即可
class Solution {
    public int maxDepth(TreeNode root) {
        //层序遍历
        if(root == null)    return 0;
        int max = 0 ;
        Deque<TreeNode> que = new LinkedList<TreeNode>();
        que.offer(root);
        while(!que.isEmpty()) {
            int size = que.size();
            ++max;
            for(int i = 0; i < size; ++i) {
                TreeNode node = que.poll();
                if(node.left!= null) que.offer(node.left);
                if(node.right!= null) que.offer(node.right);
            }
        }
        return max;
    }
}

559.n叉树的最大深度

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

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

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

示例 1:
输入:root = [1,null,3,2,4,null,5,6]
输出:3
示例 2:
输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
输出:5
提示:
树的深度不会超过 1000 。
树的节点数目位于 [0, 104] 之间。
递归
  • 递归三部曲:入参和返回值(局部变量),终止条件,循环条件
  • 入参:节点
  • 终止条件:节点为空
  • 循环条件:每循环一层,深度+1
/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public int maxDepth(Node root) {
        if(root == null) return 0 ;
        int maxDepth = 0 ;
        if(root.children != null && root.children.size()>0){
            for(Node child : root.children){
                maxDepth = Math.max(maxDepth, maxDepth(child));
            }
        }
        return maxDepth + 1;
    }
}
迭代
  • 参考二叉树的最大深度,利用层序遍历解决
class Solution {
    public int maxDepth(Node root) {
        if(root == null) return 0 ;
        int depth = 0;
        Deque<Node> que = new LinkedList<Node>();
        que.offer(root);
        while(!que.isEmpty()){
            depth++;
            int size = que.size();
            for(int i = 0; i < size; i++){
                Node node = que.poll();
                if(node.children!= null){
                    for(Node child : node.children){
                        que.offer(child);
                    }
                }
            }
        }
        return depth;
    }
}

111.二叉树的最小深度

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

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

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

递归
  • 叶子节点,左右孩子都为空的节点才是叶子节点
  • 入参:每个节点,返回值:最小深度
  • 终止条件:节点为空返回0
  • 循环条件:
    • 当左子树为空,最小深度在右子树上
    • 当右子树为空,最小深度在左子树上
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int minDepth(TreeNode root) {
        if(root == null) return 0 ;
        int minR = minDepth(root.right);
        int minL = minDepth(root.left);
        if(root.left == null)  return 1 + minR ;
        if(root.right == null) return 1 + minL ;
        return Math.min(minR, minL) + 1 ;
    }
}
迭代
  • 思想同二叉树的最大高度,区别在于当左右子树都为空时才时最小高度
  • 当得到最小高度后,不用再继续遍历树,之后的高度肯定比当前的高
class Solution {
    public int minDepth(TreeNode root) {
        if(root == null) return 0 ;
        Deque<TreeNode> que = new LinkedList<TreeNode>();
        que.offer(root);
        int depth = Integer.MAX_VALUE;
        int dep = 0 ;
        while(!que.isEmpty()){
            int size = que.size();
            dep++;
            for(int i = 0; i < size; i++){
                TreeNode node = que.poll();
                if(node.left!= null) que.offer(node.left);

                if(node.right!= null) que.offer(node.right);
				//重点:当左右子树为空时,计算一次最小高度并且之后不用再累加了
                if(node.left == null && node.right == null){
                    return Math.min(depth, dep);
                }
            }
        }
        return depth;
    }
}

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

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

完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。

示例 1:
输入:root = [1,2,3,4,5,6]
输出:6
示例 2:

输入:root = []
输出:0
示例 3:

输入:root = [1]
输出:1
提示:

树中节点的数目范围是[0, 5 * 104]
0 <= Node.val <= 5 * 104
题目数据保证输入的树是 完全二叉树
普通二叉树递归
  • 递归三部曲,不再重复
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    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) {
        if (root == null) {
            return 0;
        }
        int count = 0 ;
        Deque<TreeNode> que = new LinkedList<TreeNode>();
        que.offer(root);
        while (!que.isEmpty()) {
            int size = que.size();
            count += size;
            for (int i = 0; i < size; i++) {
                TreeNode node = que.poll();
                if (node.left!= null) {
                    que.offer(node.left);
                }
                if (node.right!= null) {
                    que.offer(node.right);
                }
            }
        }
        return count;
    }
}
完全二叉树的递归
  • 利用公式:完全二叉树的高度为h,则节点个数=2^h -1
  • 分别求出左右字数的高度,利用公式计算
    • 当左右子树都存在时,高度+1
class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int depL = 1 ,depR = 1;
        TreeNode left =  root.left;
        while(left != null){
            depL++;
            left = left.left;
        }
        TreeNode right = root.right;
        while(right!= null){
            depR++;
            right = right.right;
        }
        if(depL == depR) {
            return (1<<depL) - 1;
        }
        return countNodes(root.left) + countNodes(root.right) + 1;
    }
}

总结

  • 递归三部曲:参数和返回值、终止条件、单层循环条件
  • 层序遍历模板:队列、队列大小、循环队列大小
  • 完全二叉树性质:节点=2^h -1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值