LeetCode day27

LeetCode day27

—今天做到树,,,对不起我的数据结构老师啊~~~

7. 整数反转

给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。

如果反转后整数超过 32 位的有符号整数的范围 [−231, 231 − 1] ,就返回 0。

假设环境不允许存储 64 位整数(有符号或无符号)。

示例 1:

输入:x = 123
输出:321

示例 2:

输入:x = -123
输出:-321

示例 3:

输入:x = 120
输出:21

示例 4:

输入:x = 0
输出:0

嘛。。。用int去接直接寄掉

class Solution {
   
    public int reverse(int x) {
   
        LinkedList<Integer>queue=new LinkedList<>();
        int flag=0;
        if(x<0){
   
            flag=1;
            x=Math.abs(x);
        }
        while(x>0) {
   
            queue.offer(x%10);
            x/=10;
        }
        long curr=0;
        while(!queue.isEmpty()){
   
            curr=curr*10+queue.poll();
        }
        if(curr>Math.pow(2,31)-1){
   
            return 0;
        }
        if(flag==1){
   
            curr= -curr;
        }
        if(curr<0-Math.pow(2,32)){
   
            return 0;
        }
        return (int) curr;
    }
}

104. 二叉树的最大深度

给定一个二叉树 root ,返回其最大深度。

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

示例 1:

img

输入:root = [3,9,20,null,null,15,7]
输出:3

示例 2:

输入:root = [1,null,2]
输出:2

/**
 * 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 maxDepth(TreeNode root) {
   
             if(root==null){
   
            return 0;
        }
        return Math.max(maxDepth(root.left),maxDepth(root.right))+1;

    }
}
class Solution {
   
    public int maxDepth(TreeNode root) {
   
        if(root==null) {
   
            return 0;
        }
        LinkedList<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        int depth=0;
        while(!queue.isEmpty()){
   
            int size=queue.size();
            while(size>0){
   
               TreeNode curr= queue.poll();
                if(curr.left!=null){
   
                    queue.offer(curr.left);
                }
                if(curr.right!=null){
   
                    queue.offer(curr.right);
                }
                size--;//剪掉一个根节点
            }
            depth++;//走完一层
        }
        return depth;
    }
}

害。。。上课的时候写的非递归的深度搜索,这次收获颇深o( ̄▽ ̄)ブ

额,也许就咱单纯忘记了还没复习


相关推荐

  1. Leetcode--27

    2023-12-20 06:46:05       33 阅读
  2. 面试经典150题(27-28)

    2023-12-20 06:46:05       34 阅读
  3. 周报 | 24.4.22-24.4.28文章汇总

    2023-12-20 06:46:05       14 阅读
  4. 10.27

    2023-12-20 06:46:05       31 阅读
  5. 27. 移除元素

    2023-12-20 06:46:05       39 阅读
  6. day27 回溯(03)

    2023-12-20 06:46:05       47 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-20 06:46:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-20 06:46:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-20 06:46:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-20 06:46:05       20 阅读

热门阅读

  1. Android 版本控制工具--Git

    2023-12-20 06:46:05       48 阅读
  2. ALVR 编译 windwos && android [Streamer &Client]

    2023-12-20 06:46:05       55 阅读
  3. Stream流中anyMatch和allMatch和noneMatch的区别

    2023-12-20 06:46:05       38 阅读