提交 f533b2e0 编写于 作者: GreyZeng's avatar GreyZeng

bst

上级 f6b60295
package 数据结构.二叉树;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
// https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
// 二叉树的中序遍历
// 笔记:https://www.cnblogs.com/greyzeng/articles/15941957.html
public class LeetCode_0094_BinaryTreeInorderTraversal {
public static 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;
}
}
// 递归方式
public List<Integer> inorderTraversal1(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
List<Integer> ans = new ArrayList<>();
in(root, ans);
return ans;
}
private void in(TreeNode root, List<Integer> ans) {
if (root == null) {
return;
}
in(root.left, ans);
ans.add(root.val);
in(root.right, ans);
}
// 【非递归】中序遍历
// 第一步,整条左边界入栈。
// 第二步,弹出就收集答案。
// 第三步,来到右树上执行同第一步的操作。
// 第四步,直到栈为空。
public List<Integer> inorderTraversal2(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
List<Integer> ans = new ArrayList<>();
Deque<TreeNode> stack = new ArrayDeque<>();
TreeNode cur = root;
while (!stack.isEmpty() || cur != null) {
if (cur != null) {
// 整条左边界入栈
stack.push(cur);
cur = cur.left;
} else {
TreeNode node = stack.pop();
// 弹出收集答案
ans.add(node.val);
// 来到右树上做同样的操作
cur = node.right;
}
}
return ans;
}
// morris遍历方式
// 第二次来到自己的时候收集
public static List<Integer> inorderTraversal(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
List<Integer> ans = new ArrayList<>();
TreeNode cur = root;
TreeNode mostRight;
while (cur != null) {
mostRight = cur.left;
if (mostRight != null) {
while (mostRight.right != null && mostRight.right != cur) {
mostRight = mostRight.right;
}
if (mostRight.right == null) {
mostRight.right = cur;
cur = cur.left;
continue;
} else {
// mostRight.right = cur;
mostRight.right = null;
}
}
ans.add(cur.val);
cur = cur.right;
}
return ans;
}
}
package 数据结构.链表;
// leetcode: https://leetcode.com/problems/merge-two-sorted-lists
// https://www.cnblogs.com/greyzeng/p/7551789.html
public class LeetCode_0021_MergeTwoSortedLists {
public static class ListNode {
public int val;
public ListNode next;
}
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null || l2 == null) {
// 如果任何一个链表为空,那么直接返回另外一个链表即可
return l1 == null ? l2 : l1;
}
// 谁小谁作为头
ListNode head = l1.val > l2.val ? l2 : l1;
// t1 和 t2 表示l1和l2下一个要遍历的位置
ListNode t1 = head == l1 ? l1.next : l1;
ListNode t2 = head == l2 ? l2.next : l2;
ListNode cur = head;
while (t1 != null || t2 != null) {
if (t1 == null) {
// l1链表已经到头,剩下只需要把l2链表接入进来即可
cur.next = t2;
t2 = t2.next;
cur = cur.next;
continue;
}
if (t2 == null) {
// l2链表已经到头,剩下只需要把l2链表接入进来即可
cur.next = t1;
t1 = t1.next;
cur = cur.next;
continue;
}
// l1和l2都没有到头,那么谁小谁接入进来即可。
if (t1.val > t2.val) {
cur.next = t2;
t2 = t2.next;
} else {
cur.next = t1;
t1 = t1.next;
}
cur = cur.next;
}
return head;
}
}
package 练习题.leetcode.easy;
// leetcode: https://leetcode.com/problems/merge-two-sorted-lists
// https://www.cnblogs.com/greyzeng/p/7551789.html
public class LeetCode_0021_MergeTwoSortedLists {
public static class ListNode {
public int val;
public ListNode next;
}
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null || l2 == null) {
// 如果任何一个链表为空,那么直接返回另外一个链表即可
return l1 == null ? l2 : l1;
}
// 谁小谁作为头
ListNode head = l1.val > l2.val ? l2 : l1;
// t1 和 t2 表示l1和l2下一个要遍历的位置
ListNode t1 = head == l1 ? l1.next : l1;
ListNode t2 = head == l2 ? l2.next : l2;
ListNode cur = head;
while (t1 != null || t2 != null) {
if (t1 == null) {
// l1链表已经到头,剩下只需要把l2链表接入进来即可
cur.next = t2;
t2 = t2.next;
cur = cur.next;
continue;
}
if (t2 == null) {
// l2链表已经到头,剩下只需要把l2链表接入进来即可
cur.next = t1;
t1 = t1.next;
cur = cur.next;
continue;
}
// l1和l2都没有到头,那么谁小谁接入进来即可。
if (t1.val > t2.val) {
cur.next = t2;
t2 = t2.next;
} else {
cur.next = t1;
t1 = t1.next;
}
cur = cur.next;
}
return head;
}
}
package 练习题.leetcode.easy;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
// https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
// 二叉树的中序遍历
// 笔记:https://www.cnblogs.com/greyzeng/articles/15941957.html
public class LeetCode_0094_BinaryTreeInorderTraversal {
public static 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;
}
}
// 递归方式
public List<Integer> inorderTraversal1(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
List<Integer> ans = new ArrayList<>();
in(root, ans);
return ans;
}
private void in(TreeNode root, List<Integer> ans) {
if (root == null) {
return;
}
in(root.left, ans);
ans.add(root.val);
in(root.right, ans);
}
// 【非递归】中序遍历
// 第一步,整条左边界入栈。
// 第二步,弹出就收集答案。
// 第三步,来到右树上执行同第一步的操作。
// 第四步,直到栈为空。
public List<Integer> inorderTraversal2(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
List<Integer> ans = new ArrayList<>();
Deque<TreeNode> stack = new ArrayDeque<>();
TreeNode cur = root;
while (!stack.isEmpty() || cur != null) {
if (cur != null) {
// 整条左边界入栈
stack.push(cur);
cur = cur.left;
} else {
TreeNode node = stack.pop();
// 弹出收集答案
ans.add(node.val);
// 来到右树上做同样的操作
cur = node.right;
}
}
return ans;
}
// morris遍历方式
// 第二次来到自己的时候收集
public static List<Integer> inorderTraversal(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
List<Integer> ans = new ArrayList<>();
TreeNode cur = root;
TreeNode mostRight;
while (cur != null) {
mostRight = cur.left;
if (mostRight != null) {
while (mostRight.right != null && mostRight.right != cur) {
mostRight = mostRight.right;
}
if (mostRight.right == null) {
mostRight.right = cur;
cur = cur.left;
continue;
} else {
// mostRight.right = cur;
mostRight.right = null;
}
}
ans.add(cur.val);
cur = cur.right;
}
return ans;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册