Java每日一练(20230424) 二叉树中序遍历、交换链表节点、不同子序列

简介: Java每日一练(20230424) 二叉树中序遍历、交换链表节点、不同子序列

1. 二叉树的中序遍历

给定一个二叉树的根节点 root ,返回它的 中序 遍历。

示例 1:

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

输出:[1,3,2]


示例 2:

输入:root = []

输出:[]

示例 3:

输入:root = [1]

输出:[1]

示例 4:

输入:root = [1,2]

输出:[2,1]


示例 5:

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

输出:[1,2]


提示:

  • 树中节点数目在范围 [0, 100] 内
  • -100 <= Node.val <= 100

进阶:递归算法很简单,你可以通过迭代算法完成吗?

出处:

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

代码:

import java.util.*;
public class isSymmetric {
    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<Integer> inorderTraversal(TreeNode root) {
            List<Integer> list = new ArrayList<>();
            Stack<TreeNode> stack = new Stack<>();
            TreeNode cur = root;
            while (cur != null || !stack.isEmpty()) {
                if (cur != null) {
                    stack.push(cur);
                    cur = cur.left;
                } else {
                    cur = stack.pop();
                    list.add(cur.val);
                    cur = cur.right;
                }
            }
            return list;
        }
    }
    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 = {1,NULL,2,3};
        Vector<Integer> vec = new Vector<Integer>(Arrays.asList(nums));
        TreeNode root = createBinaryTree(vec);
        System.out.println(s.inorderTraversal(root));
        Integer[] nums2 = {1,NULL,2};
        vec = new Vector<Integer>(Arrays.asList(nums2));
        root = createBinaryTree(vec);
        System.out.println(s.inorderTraversal(root));
        Integer[] nums3 = {3,9,20,NULL,NULL,15,7};
        vec = new Vector<Integer>(Arrays.asList(nums3));
        root = createBinaryTree(vec);
        System.out.println(s.inorderTraversal(root));
    }
}

输出:

[1, 3, 2]

[1, 2]

[9, 3, 15, 20, 7]


2. 两两交换链表中的节点

给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。

你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

示例 1:

输入:head = [1,2,3,4]

输出:[2,1,4,3]


示例 2:

输入:head = []

输出:[]

示例 3:

输入:head = [1]

输出:[1]


提示:

  • 链表中节点的数目在范围 [0, 100] 内
  • 0 <= Node.val <= 100

进阶:你能在不修改链表节点值的情况下解决这个问题吗?(也就是说,仅修改节点本身。)

出处:

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

代码:

import java.util.*;
public class swapPairs {
    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
        }
    }
    public static class Solution {
        public static ListNode swapPairs(ListNode head) {
            ListNode list1 = new ListNode(0);
            list1.next = head;
            ListNode list2 = list1;
            while (head != null && head.next != null) {
                list2.next = head.next;
                head.next = list2.next.next;
                list2.next.next = head;
                list2 = list2.next.next;
                head = list2.next;
            }
            return list1.next;
        }
    }
    public static ListNode createLinkedList(int[] nums) {
        if (nums == null || nums.length == 0) {
            return null;
        }
        ListNode head = new ListNode(nums[0]);
        ListNode cur = head;
        for (int i = 1; i < nums.length; i++) {
            cur.next = new ListNode(nums[i]);
            cur = cur.next;
        }
        return head;
    }
    public static void printLinkedList(ListNode head) {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + "->");
            cur = cur.next;
        }
        System.out.println("null");
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        int[] nums = {1,2,3,4};
        ListNode head = createLinkedList(nums);
        printLinkedList(head);
        head = s.swapPairs(head);
        printLinkedList(head);
        int[] nums2 = {1,2,3,4,5};
        head = createLinkedList(nums2);
        printLinkedList(head);
        head = s.swapPairs(head);
        printLinkedList(head);
    }
}

输出:

1->2->3->4->null

2->1->4->3->null

1->2->3->4->5->null

2->1->4->3->5->null


3. 不同的子序列

给定一个字符串 s 和一个字符串 t ,计算在 s 的子序列中 t 出现的个数。

字符串的一个 子序列 是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串。(例如,"ACE" 是 "ABCDE" 的一个子序列,而 "AEC" 不是)

题目数据保证答案符合 32 位带符号整数范围。

示例 1:

输入:s = "rabbbit", t = "rabbit"

输出:3

解释:

如下图所示, 有 3 种可以从 s 中得到 "rabbit" 的方案。


示例 2:

输入:s = "babgbag", t = "bag"

输出:5

解释:

如下图所示, 有 5 种可以从 s 中得到 "bag" 的方案。


提示:

  • 0 <= s.length, t.length <= 1000
  • s 和 t 由英文字母组成

出处:

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

代码:

import java.util.*;
class numDistinct {
    public static class Solution {
        public int numDistinct(String s, String t) {
            int n1 = s.length();
            int n2 = t.length();
            int[][] dp = new int[n1 + 1][n2 + 1];
            for (int i = 0; i <= n1; i++) {
                dp[i][0] = 1;
            }
            for (int i = 1; i <= n1; i++) {
                for (int j = 1; j <= n2; j++) {
                    if (s.charAt(i - 1) == t.charAt(j - 1)) {
                        dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
                    } else {
                        dp[i][j] = dp[i - 1][j];
                    }
                }
            }
            return dp[n1][n2];
        }
    }
    public static void main(String[] args) {
        Solution sol = new Solution();
        String s = "rabbbit";
        String t = "rabbit";
        System.out.println(sol.numDistinct(s, t));
        s = "babgbag";
        t = "bag";
        System.out.println(sol.numDistinct(s, t));
   }
}

输出:

3

5


🌟 每日一练刷题专栏 🌟

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

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

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

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

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


目录
相关文章
|
存储 SQL 算法
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
|
数据库
数据结构中二叉树,哈希表,顺序表,链表的比较补充
二叉搜索树,哈希表,顺序表,链表的特点的比较
数据结构中二叉树,哈希表,顺序表,链表的比较补充
|
Python
【Leetcode刷题Python】114. 二叉树展开为链表
LeetCode上114号问题"二叉树展开为链表"的Python实现,通过先序遍历二叉树并调整节点的左右指针,将二叉树转换为先序遍历顺序的单链表。
347 3
【Leetcode刷题Python】114. 二叉树展开为链表
|
NoSQL API Redis
c++开发redis module问题之为什么在使用RedisModule_GetApi之前要通过((void**)ctx)[0]这种方式获取其地址
c++开发redis module问题之为什么在使用RedisModule_GetApi之前要通过((void**)ctx)[0]这种方式获取其地址
324 20
|
算法 Java
java使用递归及迭代方式实现前序遍历 中序遍历 后序遍历 以及实现层序遍历
java使用递归及迭代方式实现前序遍历 中序遍历 后序遍历 以及实现层序遍历
455 7
|
存储 算法
数据结构和算法学习记录——二叉树的存储结构&二叉树的递归遍历(顺序存储结构、链表存储结构、先序中序后序递归遍历)
数据结构和算法学习记录——二叉树的存储结构&二叉树的递归遍历(顺序存储结构、链表存储结构、先序中序后序递归遍历)
435 0
数据结构和算法学习记录——二叉树的存储结构&二叉树的递归遍历(顺序存储结构、链表存储结构、先序中序后序递归遍历)
|
安全 Java C++
2023-3-25 java选择题每日一练
2023-3-25 java选择题每日一练
279 1
|
存储 IDE 编译器
c++primer plus 6 读书笔记 第九章 内存模型和名称空间
c++primer plus 6 读书笔记 第九章 内存模型和名称空间
212 0
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
755 12