代码随想录day17|104. 二叉树的最大深度

104. 二叉树的最大深度

104. 二叉树的最大深度

  • 分析:递归思路(DFS),采取中左右的前序遍历方式,每次遍历到不是空节点的树节点时,计数器cnt做加一,然后立即与res作比较取最大值,之后向左右递归。层序思路(BFS),维护双端队列,每次遍历一层做加一,最终返回答案即可。

  • 代码:

//递归DFS
int result = 0;
public int maxDepth(TreeNode root) {
    traversal(root, 0);
    return result;
}
void traversal(TreeNode root, int cnt){
    if(root == null){
        return;
    }
    cnt += 1;
    result = Math.max(result, cnt);
    traversal(root.left, cnt);
    traversal(root.right, cnt);
}

//层序BFS
public int maxDepth(TreeNode root) {
    if(root == null){
        return 0;
    }
    Deque<TreeNode> que = new LinkedList<>();
    que.add(root);
    int count = 0;
    while(!que.isEmpty()){
        int len = que.size();
        for(int i = 0; i < len; i++){
            TreeNode node = que.pollFirst();
            if(node.left != null) que.add(node.left);
            if(node.right != null) que.add(node.right);
        }
        count++;
    }
    return count;
}

111. 二叉树的最小深度

111. 二叉树的最小深度

  • 分析:递归DFS,每次遍历到非空节点计数做加加,但是只有当当前节点是叶子节点时,将当前cnt与res取最小值,最终返回答案。层序BFS,同样的遍历节点,当该节点的下一节点为空时,记录一个bool值,该层遍历完后,若bool为true,则将count直接返回。

  • 代码:

//DFS
int res = Integer.MAX_VALUE;
public int minDepth(TreeNode root) {
    if(root == null){
        return 0;
    }
    traversal(root, 0);
    return res;
}
void traversal(TreeNode root, int cnt){
    if(root == null){
        return;
    }
    cnt += 1;
    if(root.left == null && root.right == null){
        res = Math.min(res, cnt);
    }
    traversal(root.left, cnt);
    traversal(root.right, cnt);
}

//BFS
public int minDepth(TreeNode root) {
    if(root == null){
        return 0;
    }
    Deque<TreeNode> que = new LinkedList<>();
    que.add(root);
    int count = 0;
    while(!que.isEmpty()){
        int len = que.size();
        boolean now = false;
        for(int i = 0; i < len; i++){
            TreeNode node = que.pollFirst();
            if(node.left != null) {
                que.add(node.left);
            }
            if(node.right != null) {
                que.add(node.right);
            }
            if(node.left == null && node.right == null){
                now = true;
            }
        }
        count++;
        if(now){
            break;
        }
    }
    return count;
}

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

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

  • 分析:统计节点个数,DFS,后序遍历,当前节点个数等于左子树个数加右子树个数加一。层序遍历,每次遍历都做加加即可。

  • 代码:

//DFS:
public int countNodes(TreeNode root) {
    if(root == null){
        return 0;
    }
    int left = countNodes(root.left);
    int right = countNodes(root.right);
    return left + right + 1;
}
//BFS:
public int countNodes(TreeNode root) {
    if(root == null){
        return 0;
    }
    Deque<TreeNode> que = new LinkedList<>();
    que.add(root);
    int res = 0;
    while(!que.isEmpty()){
        int len = que.size();
        for(int i = 0; i < len; i++){
            TreeNode node = que.pollFirst();
            res++;
            if(node.left != null) que.add(node.left);
            if(node.right != null) que.add(node.right);
        }
    }

    return res;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值