九日集训第九日(简单递归)

一、前言

九日集训第九天

二、题目

1)172. 阶乘后的零

  给定一个整数 n n n ,返回 n ! n! n! 结果中尾随零的数量。

1.a)题目分析:

  本题可以找 n n n是不是 5 5 5倍数,我们就可以找到 5 5 5的倍数,但不是 25 25 25的倍数,然后找 25 25 25的倍数,但不是 125 125 125的倍数。

1.b)代码:

class Solution {
    public int trailingZeroes(int n) {
    if(n<5){
        return 0;
    }
    return n/5+trailingZeroes(n/5);
    }
}

2)1342. 将数字变成 0 的操作次数

  给你一个非负整数 n u m num num ,请你返回将它变成 0 0 0 所需要的步数。 如果当前数字是偶数,你需要把它除以 2 2 2 ;否则,减去 1 1 1

2.a)题目分析:

  运用递归来解决本题即可。

2.b)代码:

class Solution {
    public int numberOfSteps(int num) {
       if(num==0){
           return 0;
       }
       if(num%2==0){
           return numberOfSteps(num/2)+1;
       }
       else{
           return numberOfSteps(num-1)+1;
       }

    }
}

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

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

3.a)题目分析:

  先定义递归终点 ( r o o t = = n u l l ) (root==null) root==null然后运用递归解决即可。

3.b)代码:

class Solution {
    public int countNodes(TreeNode root) {
        return root == null ? 0 : 1 + countNodes(root.left) + countNodes(root.right);
    }
}

4)LCP 44. 开幕式焰火

  力扣挑战赛」开幕式开始了,空中绽放了一颗二叉树形的巨型焰火。给定一棵二叉树 r o o t root root 代表焰火,节点值表示巨型焰火这一位置的颜色种类。请帮小扣计算巨型焰火有多少种不同的颜色。

4.a)题目分析:

  本题采用先序遍历+哈希表来解决。

4.b)代码:

class Solution {
    HashSet<Integer> hashSet = new HashSet<>();
    public int numColor(TreeNode root) {
        dfs(root);
        return hashSet.size();
    }

    public void dfs(TreeNode root){
        if(root==null)
        {
            return;
        }
        if(!hashSet.contains(root.val)){
        	hashSet.add(root.val);
        }
        dfs(root.left);
        dfs(root.right);
        
    }
}

5)397. 整数替换

  给定一个正整数 n n n ,你可以做如下操作:
    如果 n n n 是偶数,则用 n / 2 n / 2 n/2替换 n n n
    如果 n n n 是奇数,则可以用 n + 1 n + 1 n+1 n − 1 n - 1 n1替换 n n n
  返回 n n n 变为 1 1 1 所需的 最小替换次数 。

5.a)题目分析:

  本题可以使用递归来解题,但要注意当 n n n为奇数的时候,要保证步数最少,我们可以将奇数分成两步来走,取这两步中的最小值。

5.b)代码:

class Solution {
    public int integerReplacement(int n) {
        if (n == 1) {
            return 0;
        }
        if (n % 2 == 0) {
            return 1 + integerReplacement(n / 2);
        }
        return 2 + Math.min(integerReplacement(n / 2), integerReplacement(n / 2 + 1));
    }
}

6)938. 二叉搜索树的范围和

  给定二叉搜索树的根结点 r o o t root root,返回值位于范围 [ l o w , h i g h ] [low, high] [low,high] 之间的所有结点的值的和。

6.a)题目分析:

  

6.b)代码:

class Solution {
    int ans =0;
    public int rangeSumBST(TreeNode root, int low, int high) {
        dfs(root,low,high);
        return ans;
    }
    public void dfs(TreeNode root, int low, int high){
        if(root==null){
            return;
        }
        if(low <= root.val && root.val <= high){
            ans+=root.val;
        }
        dfs(root.left,low,high);
        dfs(root.right,low,high);
    }
}

7)剑指 Offer 55 - I. 二叉树的深度

  输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点依次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。

7.a)题目分析:

   本题可以使用深度优先搜索来解决。

7.b)代码:

class Solution {
    int res =0;
    public int maxDepth(TreeNode root) {
        dfs(root,1);
        return res;
    }
    public void dfs(TreeNode root,int height){
        if(root==null){
            return;
        }
        if(root.left == null && root.right == null){
           res = Math.max(res,height);
       }
        dfs(root.left,height+1);
        dfs(root.right,height+1);
    }
}

8)104. 二叉树的最大深度

  给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

8.a)题目分析:

   本题跟上一题相同

8.b)代码:

class Solution {
    int res =0;
    public int maxDepth(TreeNode root) {
        dfs(root,1);
        return res;
    }
    public void dfs(TreeNode root,int height){
        if(root==null){
            return;
        }
        if(root.left == null && root.right == null){
           res = Math.max(res,height);
       }
        dfs(root.left,height+1);
        dfs(root.right,height+1);
    }
}

9)226. 翻转二叉树

  给你一棵二叉树的根节点 r o o t root root ,翻转这棵二叉树,并返回其根节点。

9.a)题目分析:

   本题可以使用 b f s bfs bfs广度优先搜索来解决。

9.b)代码:

class Solution {
    public TreeNode invertTree(TreeNode root) {
        bfs(root);
        return root;
    }
    public void bfs(TreeNode root){
        if(root==null){
            return;
        }
        TreeNode temp;
        temp=root.left;
        root.left=root.right;
        root.right=temp;
        bfs(root.left);
        bfs(root.right);
    }
}

10)797. 所有可能的路径

  给你一个有 n n n 个节点的 有向无环图 ( D A G ) (DAG) DAG,请你找出所有从节点 0 0 0 到节点 n − 1 n-1 n1 的路径并输出(不要求按特定顺序) g r a p h [ i ] graph[i] graph[i] 是一个从节点 i i i 可以访问的所有节点的列表(即从节点 i i i 到节点 g r a p h [ i ] [ j ] graph[i][j] graph[i][j]存在一条有向边)。

10.a)题目分析:

   本题可以使用深度优先搜索来解决

10.b)代码:

class Solution {
    List<List<Integer>> ans = new ArrayList<List<Integer>>();
    Deque<Integer> stack = new ArrayDeque<Integer>();

    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        stack.offerLast(0);
        dfs(graph, 0, graph.length - 1);
        return ans;
    }

    public void dfs(int[][] graph, int x, int n) {
        if (x == n) {
            ans.add(new ArrayList<Integer>(stack));
            return;
        }
        for (int y : graph[x]) {
            stack.offerLast(y);
            dfs(graph, y, n);
            stack.pollLast();
        }
    }
}

11)剑指 Offer II 110. 所有路径

  给你一个有 n n n 个节点的 有向无环图 ( D A G ) (DAG) DAG,请你找出所有从节点 0 0 0 到节点 n − 1 n-1 n1 的路径并输出(不要求按特定顺序) g r a p h [ i ] graph[i] graph[i] 是一个从节点 i i i 可以访问的所有节点的列表(即从节点 i i i 到节点 g r a p h [ i ] [ j ] graph[i][j] graph[i][j]存在一条有向边)。

11.a)题目分析:

   本题与上题相同

11.b)代码:

class Solution {
    List<List<Integer>> ans = new ArrayList<List<Integer>>();
    Deque<Integer> stack = new ArrayDeque<Integer>();

    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        stack.offerLast(0);
        dfs(graph, 0, graph.length - 1);
        return ans;
    }

    public void dfs(int[][] graph, int x, int n) {
        if (x == n) {
            ans.add(new ArrayList<Integer>(stack));
            return;
        }
        for (int y : graph[x]) {
            stack.offerLast(y);
            dfs(graph, y, n);
            stack.pollLast();
        }
    }
}

三、做题记录

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枏念

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值