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

## 1. 二叉树的中序遍历

• 树中节点数目在范围 [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();
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. 两两交换链表中的节点

• 链表中节点的数目在范围 [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);
ListNode list2 = list1;
while (head != null && head.next != null) {
list2 = list2.next.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;
}
}
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};
int[] nums2 = {1,2,3,4,5};
}
}

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

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

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

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

## 3. 不同的子序列

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

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

## 🌟 每日一练刷题专栏 🌟

👍 点赞，你的认可是我坚持的动力！

🌟 收藏，你的青睐是我努力的方向！

|
3天前
|

【优选算法专栏】专题九：链表--------两两交换链表中的节点
【优选算法专栏】专题九：链表--------两两交换链表中的节点
14 0
|
1月前
|
Java
CSDN每日一练（Java）--小艺的英文名
CSDN每日一练（Java）--小艺的英文名
18 0
|
1月前
|

34 1
|
21天前
|

【C++ 14 新特性】C++14二进制字面量：深度探索与实践
【C++ 14 新特性】C++14二进制字面量：深度探索与实践
36 1
|
21天前
|

【C++ 函数 基本教程 第六篇 】深度解析C++函数符号：GCC与VS的名称修饰揭秘
【C++ 函数 基本教程 第六篇 】深度解析C++函数符号：GCC与VS的名称修饰揭秘
36 1
|
1月前
leetcode2487.从链表中移除节点
leetcode2487.从链表中移除节点
18 1
|
1月前
|
C语言
【C语言】Leetcode 876. 链表的中间节点
【C语言】Leetcode 876. 链表的中间节点
15 0
|
1月前
|

20 1
|
18天前
|

C++入门指南：string类文档详细解析（非常经典，建议收藏）
C++入门指南：string类文档详细解析（非常经典，建议收藏）
31 0
|
18天前
|

C++入门: 类和对象笔记总结（上）
C++入门: 类和对象笔记总结（上）
30 0