Java每日一练(20230419) 二叉树的最大深度、层序遍历、最短回文串

简介: Java每日一练(20230419) 二叉树的最大深度、层序遍历、最短回文串

1. 二叉树的最大深度

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

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

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

示例:

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

    3

   / \

  9  20

/  \

15   7

返回它的最大深度 3 。

出处:

https://edu.csdn.net/practice/25949981

代码:

import java.util.*;
public class maxDepth {
    public final static int NULL = Integer.MIN_VALUE; //用NULL来表示空节点
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) {
            val = x;
        }
    }
    public static class Solution {
        public int maxDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int deep = 1;
            if (root.left == null && root.right == null) {
                return 1;
            }
            int leftDeep = 0;
            if (root.left != null) {
                leftDeep = 1 + maxDepth(root.left);
            }
            int rightDeep = 0;
            if (root.right != null) {
                rightDeep = 1 + maxDepth(root.right);
            }
            return deep + leftDeep > rightDeep ? leftDeep : rightDeep;
        }
    }
    public static TreeNode createBinaryTree(Vector<Integer> vec) {
        if (vec == null || vec.size() == 0) {
            return null;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode root = new TreeNode(vec.get(0));
        queue.offer(root);
        int i = 1;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int j = 0; j < size; j++) {
                TreeNode node = queue.poll();
                if (i < vec.size() && vec.get(i) != NULL) {
                    node.left = new TreeNode(vec.get(i));
                    queue.offer(node.left);
                }
                i++;
                if (i < vec.size() && vec.get(i) != NULL) {
                    node.right = new TreeNode(vec.get(i));
                    queue.offer(node.right);
                }
                i++;
            }
        }
        return root;
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        Integer[] nums = {3,9,20,NULL,NULL,15,7};
        Vector<Integer> vec = new Vector<Integer>(Arrays.asList(nums));
        TreeNode root = createBinaryTree(vec);
        System.out.println(s.maxDepth(root));
    }
}

输出:

3


2. 二叉树的层序遍历

给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。

示例:

二叉树:[3,9,20,null,null,15,7],

    3

   / \

  9  20

/  \

15   7


返回其层序遍历结果:

[

[3],

[9,20],

[15,7]

]

出处:

https://edu.csdn.net/practice/25855085

代码:

import java.util.*;
public class levelOrder {
    public final static int NULL = Integer.MIN_VALUE; //用NULL来表示空节点
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) {
            val = x;
        }
    }
    public static class Solution {
        public List<List<Integer>> levelOrder(TreeNode root) {
            List<List<Integer>> l = new ArrayList<>();
            Queue<TreeNode> q = new LinkedList<TreeNode>();
            if (root != null) {
                q.add(root);
            }
            while (!q.isEmpty()) {
                List<Integer> l2 = new ArrayList<>();
                int number = q.size();
                while (number > 0) {
                    TreeNode t = q.poll();
                    l2.add(t.val);
                    if (t.left != null) {
                        q.add(t.left);
                    }
                    if (t.right != null) {
                        q.add(t.right);
                    }
                    number--;
                }
                l.add(l2);
            }
            return l;
        }
    }
    public static TreeNode createBinaryTree(Vector<Integer> vec) {
        if (vec == null || vec.size() == 0) {
            return null;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode root = new TreeNode(vec.get(0));
        queue.offer(root);
        int i = 1;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int j = 0; j < size; j++) {
                TreeNode node = queue.poll();
                if (i < vec.size() && vec.get(i) != NULL) {
                    node.left = new TreeNode(vec.get(i));
                    queue.offer(node.left);
                }
                i++;
                if (i < vec.size() && vec.get(i) != NULL) {
                    node.right = new TreeNode(vec.get(i));
                    queue.offer(node.right);
                }
                i++;
            }
        }
        return root;
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        Integer[] nums = {3,9,20,NULL,NULL,15,7};
        Vector<Integer> vec = new Vector<Integer>(Arrays.asList(nums));
        TreeNode root = createBinaryTree(vec);
        System.out.println(s.levelOrder(root));
    }
}

输出:

[[3], [9, 20], [15, 7]]


3. 最短回文串

给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。

示例 1:

输入:s = "aacecaaa"

输出:"aaacecaaa"


示例 2:

输入:s = "abcd"

输出:"dcbabcd"


提示:

  • 0 <= s.length <= 5 * 10^4
  • s 仅由小写英文字母组成

出处:

https://edu.csdn.net/practice/25949983

代码:

import java.util.*;
public class shortestPalindrome {
    public static class Solution {
        public static String shortestPalindrome(String s) {
            StringBuilder r = new StringBuilder(s).reverse();
            String str = s + "#" + r;
            int[] next = next(str);
            String prefix = r.substring(0, r.length() - next[str.length()]);
            return prefix + s;
        }
        private static int[] next(String P) {
            int[] next = new int[P.length() + 1];
            next[0] = -1;
            int k = -1;
            int i = 1;
            while (i < next.length) {
                if (k == -1 || P.charAt(k) == P.charAt(i - 1)) {
                    next[i++] = ++k;
                } else {
                    k = next[k];
                }
            }
            return next;
        }
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        String str = "aacecaaa";
        System.out.println(s.shortestPalindrome(str));
        str = "abcd";
        System.out.println(s.shortestPalindrome(str));
    }
}

输出:

aaacecaaa

dcbabcd


🌟 每日一练刷题专栏 🌟

持续,努力奋斗做强刷题搬运工!

👍 点赞,你的认可是我坚持的动力!

🌟 收藏,你的青睐是我努力的方向!

评论,你的意见是我进步的财富!  

主页:https://hannyang.blog.csdn.net/


目录
相关文章
|
存储 人工智能 运维
【01】做一个精美的打飞机小游戏,浅尝阿里云通义灵码python小游戏开发AI编程-之飞机大战小游戏上手实践-优雅草央千澈-用ai开发小游戏尝试-分享源代码和游戏包
【01】做一个精美的打飞机小游戏,浅尝阿里云通义灵码python小游戏开发AI编程-之飞机大战小游戏上手实践-优雅草央千澈-用ai开发小游戏尝试-分享源代码和游戏包
782 48
【01】做一个精美的打飞机小游戏,浅尝阿里云通义灵码python小游戏开发AI编程-之飞机大战小游戏上手实践-优雅草央千澈-用ai开发小游戏尝试-分享源代码和游戏包
|
人工智能 Python
【02】做一个精美的打飞机小游戏,python开发小游戏-鹰击长空—优雅草央千澈-持续更新-分享源代码和游戏包供游玩-记录完整开发过程-用做好的素材来完善鹰击长空1.0.1版本
【02】做一个精美的打飞机小游戏,python开发小游戏-鹰击长空—优雅草央千澈-持续更新-分享源代码和游戏包供游玩-记录完整开发过程-用做好的素材来完善鹰击长空1.0.1版本
830 7
|
9月前
|
小程序 PHP 图形学
热门小游戏源码(Python+PHP)下载-微信小程序游戏源码Unity发实战指南​
本文详解如何结合Python、PHP与Unity开发并部署小游戏至微信小程序。涵盖技术选型、Pygame实战、PHP后端对接、Unity转换适配及性能优化,提供从原型到发布的完整指南,助力开发者快速上手并发布游戏。
|
11月前
|
存储 算法 区块链
从零实现Python扫雷游戏:完整开发指南与深度解析
扫雷作为Windows经典游戏,承载了许多人的童年回忆。本文将详细介绍如何使用Python和Tkinter库从零开始构建一个功能完整的扫雷游戏,涵盖游戏设计、算法实现和界面开发的全过程。
787 1
|
12月前
|
人工智能 搜索推荐 数据可视化
用 Python 制作简单小游戏教程:手把手教你开发猜数字游戏
本教程详细讲解了用Python实现经典猜数字游戏的完整流程,涵盖从基础规则到高级功能的全方位开发。内容包括游戏逻辑设计、输入验证与错误处理、猜测次数统计、难度选择、彩色输出等核心功能,并提供完整代码示例。同时,介绍了开发环境搭建及调试方法,帮助初学者快速上手。最后还提出了图形界面、网络对战、成就系统等扩展方向,鼓励读者自主创新,打造个性化游戏版本。适合Python入门者实践与进阶学习。
1363 1
|
12月前
|
存储 算法 数据可视化
用Python开发猜数字游戏:从零开始的手把手教程
猜数字游戏是编程入门经典项目,涵盖变量、循环、条件判断等核心概念。玩家通过输入猜测电脑生成的随机数,程序给出提示直至猜中。项目从基础实现到功能扩展,逐步提升难度,适合各阶段Python学习者。
940 0
|
测试技术 Python
【03】做一个精美的打飞机小游戏,规划游戏项目目录-分门别类所有的资源-库-类-逻辑-打包为可玩的exe-练习python打包为可执行exe-优雅草卓伊凡-持续更新-分享源代码和游戏包供游玩-1.0.2版本
【03】做一个精美的打飞机小游戏,规划游戏项目目录-分门别类所有的资源-库-类-逻辑-打包为可玩的exe-练习python打包为可执行exe-优雅草卓伊凡-持续更新-分享源代码和游戏包供游玩-1.0.2版本
821 31
【03】做一个精美的打飞机小游戏,规划游戏项目目录-分门别类所有的资源-库-类-逻辑-打包为可玩的exe-练习python打包为可执行exe-优雅草卓伊凡-持续更新-分享源代码和游戏包供游玩-1.0.2版本
|
Python
课程设计项目之基于Python实现围棋游戏代码
游戏进去默认为九路玩法,当然也可以选择十三路或是十九路玩法 使用pycharam打开项目,pip安装模块并引用,然后运行即可, 代码每行都有详细的注释,可以做课程设计或者毕业设计项目参考
417 33
|
Java
【用Java学习数据结构系列】震惊,二叉树原来是要这么学习的(二)
【用Java学习数据结构系列】震惊,二叉树原来是要这么学习的(二)
196 1

推荐镜像

更多