【力扣刷题】Day16——二叉树专题


上一篇文章:【力扣刷题】Day15——二叉树专题_塔塔开!!!的博客-CSDN博客

6. 二叉树的最大深度

题目链接:104. - 力扣(LeetCode)

思路:dfs,前序遍历统计最大深度即可

static遇到的坑!!!

Code

 /**
    dfs:前序遍历求二叉树的最大深度
  */
class Solution {
    int res = -0x3f3f3f3f;
    public int maxDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        dfs(root, 0);
        return res;
    }
    public void dfs(TreeNode root, int depth){
        if(root == null){
            return ;
        }
        depth ++;
        res = Math.max(res, depth);
        dfs(root.left, depth);
        dfs(root.right, depth);
    }
}

8. 二叉树的最小深度

题目链接:111. 二叉树的最小深度 - 力扣(LeetCode)

二叉树的最小深度:当这个节点的左右儿子都为空时此时的深度才加入最小深度的比较计算!

思路:计算出所有满足条件的深度(左右儿子的深度)—— 当只有左右儿子都为空时才算的是能比较的深度,比较取最小即可(左子树的最小深度和右子树的最小深度比较)

Code

class Solution {
    int res = 0x3f3f3f3f;
    public int minDepth(TreeNode root) {
       if(root == null){
           return 0;
       }
        dfs(root, 0);
        return res;
    }
    public void dfs(TreeNode root, int depth){
        if(root == null){// 递归出口
            return ;
        }
        depth ++;
        if(root.left == null && root.right == null){
            res = Math.min(res, depth);
        }
        dfs(root.left,  depth);
        dfs(root.right,  depth);
    }
}

7. N 叉树的最大深度

题目链接:559. N 叉树的最大深度 - 力扣(LeetCode)

二叉树的最大深度扩展到N叉树,思路是一样的递归到最后一个节点然后比较,不同的是上一题是只有两棵子树,为此我们求出所有子树的最大深度然后比较即可。

Code

class Solution {
    int res = -0x3f3f3f3f;
    public int maxDepth(Node root) {
        if(root == null){
            return 0;
        }
        dfs(root, 0);
        return res;
    }
    public void dfs(Node root, int depth){
        if(root == null){
            return ;
        }
        // 遍历所有子树 求子树的最大深度(之前我们是只遍历左右子树)
        depth ++;
        res = Math.max(res, cnt);
        List<Node> C = root.children;
        for(Node child : C){
            dfs(child, cnt );
        }
    }
}

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

题目链接:222. 完全二叉树的节点个数 - 力扣(LeetCode)222. 完全二叉树的节点个数 - 力扣(LeetCode)

解法一:BFS层序遍历统计节点数

Code

 // 思路一:直接BFS拿到所有节点
class Solution {
    public int countNodes(TreeNode root) {
        if(root == null){
            return 0;
        }
        return bfs(root);
    }
    public int bfs(TreeNode root){
        List<TreeNode> list = new ArrayList<>();
        Queue<TreeNode> q = new LinkedList<>();
        q.add(root);
        while(!q.isEmpty()){
            int len = q.size();
            for(int i = 0; i < len; i ++){
                TreeNode t = q.poll();
                list.add(t);
                if(t.left != null){
                    q.add(t.left);
                }
                if(t.right != null){
                    q.add(t.right);
                }
            }
        }
        return list.size();
    }
}

解法二:递归求解

求一棵完全二叉树的节点个数,其实就是递归遍历求左子树和右子树的个数,它们两个相加再加上根节点的个数 11,就是完全二叉树的节点数。

class Solution {
    public int countNodes(TreeNode root) {
        return dfs(root);
    }
    /**
        返回一个棵树的所有节点数
     */
    public int dfs(TreeNode root){
        if(root == null){
            return 0;
        }
        return dfs(root.left) + dfs(root.right) + 1;
    }
}

解法三:dfs深度优先搜索统计节点数

// 思路三:直接dfs深度优先搜索统计即可
class Solution {
    int res = 0;// 开的全局变量!!!!!!
    public int countNodes(TreeNode root) {
        if(root == null){
            return 0;
        }
        dfs(root);
        return res;
    }
    public void dfs(TreeNode root){
        if(root == null){
            return ;
        }
        cnt ++;
        dfs(root.left);
        dfs(root.right);
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于力扣刷C++常用操作,我可以给你一些常见的操作和技巧: 1. 使用 STL 容器和算法库:STL(Standard Template Library)是 C++ 标准库中的一个重要组成部分,包含了许多常用的容器和算法。在力扣刷中,使用 STL 可以大大提高代码的效率和可读性。例如,vector 可以用来存储动态数组,sort 可以用来排序等等。 2. 使用 auto 关键字:auto 关键字可以自动推导变量类型,可以减少代码量和提高可读性。例如,auto x = 1; 可以自动推导出 x 的类型为 int。 3. 使用 lambda 表达式:lambda 表达式是 C++11 中引入的一种匿名函数,可以方便地定义一些简单的函数对象。在力扣刷中,使用 lambda 表达式可以简化代码,例如在 sort 函数中自定义比较函数。 4. 使用位运算:位运算是一种高效的运算方式,在力扣刷中经常会用到。例如,左移运算符 << 可以用来计算 2 的幂次方,右移运算符 >> 可以用来除以 2 等等。 5. 使用递归:递归是一种常见的算法思想,在力扣刷中也经常会用到。例如,二叉树的遍历、链表的反转等等。 6. 使用 STL 中的 priority_queue:priority_queue 是 STL 中的一个容器,可以用来实现堆。在力扣刷中,使用 priority_queue 可以方便地实现一些需要维护最大值或最小值的算法。 7. 使用 STL 中的 unordered_map:unordered_map 是 STL 中的一个容器,可以用来实现哈希表。在力扣刷中,使用 unordered_map 可以方便地实现一些需要快速查找和插入的算法。 8. 使用 STL 中的 string:string 是 STL 中的一个容器,可以用来存储字符串。在力扣刷中,使用 string 可以方便地处理字符串相关的问。 9. 注意边界条件:在力扣刷中,边界条件往往是解决问的关键。需要仔细分析目,考虑各种边界情况,避免出现错误。 10. 注意时间复杂度:在力扣刷中,时间复杂度往往是评判代码优劣的重要指标。需要仔细分析算法的时间复杂度,并尽可能优化代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值