Skip to content

Commit ce4c4e1

Browse files
committed
add class 06
1 parent 98abdf2 commit ce4c4e1

10 files changed

+314
-2
lines changed

src/class06/Code05_Comparator.java renamed to src/class06/Code01_Comparator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.Comparator;
66
import java.util.TreeMap;
77

8-
public class Code05_Comparator {
8+
public class Code01_Comparator {
99

1010
public static class Student {
1111
public String name;

src/class06/Code06_MergeKSortedLists.java renamed to src/class06/Code02_MergeKSortedLists.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.PriorityQueue;
55

66
// 测试链接:https://leetcode.com/problems/merge-k-sorted-lists/
7-
public class Code06_MergeKSortedLists {
7+
public class Code02_MergeKSortedLists {
88

99
public static class ListNode {
1010
public int val;

src/class06/Code03_SameTree.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package class06;
2+
3+
// 测试链接:https://leetcode.com/problems/same-tree
4+
public class Code03_SameTree {
5+
6+
public static class TreeNode {
7+
public int val;
8+
public TreeNode left;
9+
public TreeNode right;
10+
}
11+
12+
public static boolean isSameTree(TreeNode p, TreeNode q) {
13+
if (p == null ^ q == null) {
14+
return false;
15+
}
16+
if (p == null && q == null) {
17+
return true;
18+
}
19+
return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
20+
}
21+
22+
}

src/class06/Code04_SymmetricTree.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package class06;
2+
3+
// 测试链接:https://leetcode.com/problems/symmetric-tree
4+
public class Code04_SymmetricTree {
5+
6+
public static class TreeNode {
7+
public int val;
8+
public TreeNode left;
9+
public TreeNode right;
10+
}
11+
12+
public static boolean isSymmetric(TreeNode root) {
13+
return isMirror(root, root);
14+
}
15+
16+
public static boolean isMirror(TreeNode h1, TreeNode h2) {
17+
if (h1 == null ^ h2 == null) {
18+
return false;
19+
}
20+
if (h1 == null && h2 == null) {
21+
return true;
22+
}
23+
return h1.val == h2.val && isMirror(h1.left, h2.right) && isMirror(h1.right, h2.left);
24+
}
25+
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package class06;
2+
3+
// 测试链接:https://leetcode.com/problems/maximum-depth-of-binary-tree
4+
public class Code05_MaximumDepthOfBinaryTree {
5+
6+
public static class TreeNode {
7+
public int val;
8+
public TreeNode left;
9+
public TreeNode right;
10+
}
11+
12+
public static int maxDepth(TreeNode root) {
13+
if (root == null) {
14+
return 0;
15+
}
16+
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
17+
}
18+
19+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package class06;
2+
3+
import java.util.HashMap;
4+
5+
public class Code06_ConstructBinaryTreeFromPreorderAndInorderTraversal {
6+
7+
public static class TreeNode {
8+
int val;
9+
TreeNode left;
10+
TreeNode right;
11+
12+
TreeNode(int val) {
13+
this.val = val;
14+
}
15+
}
16+
17+
public static TreeNode buildTree(int[] preorder, int[] inorder) {
18+
HashMap<Integer, Integer> inMap = new HashMap<>();
19+
for (int i = 0; i < inorder.length; i++) {
20+
inMap.put(inorder[i], i);
21+
}
22+
return process(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1, inMap);
23+
}
24+
25+
public static TreeNode process(int[] pre, int L1, int R1, int[] in, int L2, int R2,
26+
HashMap<Integer, Integer> inMap) {
27+
if (L1 > R1) {
28+
return null;
29+
}
30+
TreeNode head = new TreeNode(pre[L1]);
31+
if (L1 == R1) {
32+
return head;
33+
}
34+
int find = inMap.get(pre[L1]);
35+
head.left = process(pre, L1 + 1, L1 + find - L2, in, L2, find - 1, inMap);
36+
head.right = process(pre, L1 + find - L2 + 1, R1, in, find + 1, R2, inMap);
37+
return head;
38+
}
39+
40+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package class06;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
import java.util.Queue;
6+
7+
// 测试链接:https://leetcode.com/problems/binary-tree-level-order-traversal-ii
8+
public class Code07_BinaryTreeLevelOrderTraversalII {
9+
10+
public static class TreeNode {
11+
public int val;
12+
public TreeNode left;
13+
public TreeNode right;
14+
15+
TreeNode(int val) {
16+
this.val = val;
17+
}
18+
}
19+
20+
public List<List<Integer>> levelOrderBottom(TreeNode root) {
21+
List<List<Integer>> ans = new LinkedList<>();
22+
if (root == null) {
23+
return ans;
24+
}
25+
Queue<TreeNode> queue = new LinkedList<>();
26+
queue.add(root);
27+
while (!queue.isEmpty()) {
28+
int size = queue.size();
29+
List<Integer> curAns = new LinkedList<>();
30+
for (int i = 0; i < size; i++) {
31+
TreeNode curNode = queue.poll();
32+
curAns.add(curNode.val);
33+
if (curNode.left != null) {
34+
queue.add(curNode.left);
35+
}
36+
if (curNode.right != null) {
37+
queue.add(curNode.right);
38+
}
39+
}
40+
ans.add(0, curAns);
41+
}
42+
return ans;
43+
}
44+
45+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package class06;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
import java.util.Queue;
6+
7+
public class Code08_AverageOfLevelsInBinaryTree {
8+
9+
public static class TreeNode {
10+
public int val;
11+
public TreeNode left;
12+
public TreeNode right;
13+
14+
TreeNode(int val) {
15+
this.val = val;
16+
}
17+
}
18+
19+
public static List<Double> averageOfLevels(TreeNode root) {
20+
List<Double> ans = new LinkedList<>();
21+
if (root == null) {
22+
return ans;
23+
}
24+
Queue<TreeNode> queue = new LinkedList<>();
25+
queue.add(root);
26+
while (!queue.isEmpty()) {
27+
int size = queue.size();
28+
double sum = 0D;
29+
for (int i = 0; i < size; i++) {
30+
TreeNode curNode = queue.poll();
31+
sum += curNode.val;
32+
if (curNode.left != null) {
33+
queue.add(curNode.left);
34+
}
35+
if (curNode.right != null) {
36+
queue.add(curNode.right);
37+
}
38+
}
39+
ans.add(sum / size);
40+
}
41+
return ans;
42+
}
43+
44+
}

src/class06/Code09_GetMax.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package class06;
2+
3+
public class Code09_GetMax {
4+
5+
public static int flip(int n) {
6+
return n ^ 1;
7+
}
8+
9+
public static int sign(int n) {
10+
11+
return flip((n >> 31) & 1);
12+
}
13+
14+
public static int getMax1(int a, int b) {
15+
int c = a - b;
16+
int scA = sign(c);
17+
int scB = flip(scA);
18+
return a * scA + b * scB;
19+
}
20+
21+
public static int getMax2(int a, int b) {
22+
int c = a - b;
23+
int sa = sign(a);
24+
int sb = sign(b);
25+
int sc = sign(c);
26+
int difSab = sa ^ sb;
27+
int sameSab = flip(difSab);
28+
int returnA = difSab * sa + sameSab * sc;
29+
int returnB = flip(returnA);
30+
return a * returnA + b * returnB;
31+
}
32+
33+
public static void main(String[] args) {
34+
int a = -16;
35+
int b = -19;
36+
System.out.println(getMax1(a, b));
37+
System.out.println(getMax2(a, b));
38+
a = 2147483647;
39+
b = -2147480000;
40+
System.out.println(getMax1(a, b)); // wrong answer because of overflow
41+
System.out.println(getMax2(a, b));
42+
}
43+
44+
}

src/class06/TraversalBT.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package class06;
2+
3+
public class TraversalBT {
4+
5+
public static class Node {
6+
public int value;
7+
public Node left;
8+
public Node right;
9+
10+
public Node(int v) {
11+
value = v;
12+
}
13+
}
14+
15+
public static void f(Node head) {
16+
if (head == null) {
17+
return;
18+
}
19+
// 1
20+
f(head.left);
21+
// 2
22+
f(head.right);
23+
// 3
24+
}
25+
26+
// 先序打印所有节点
27+
public static void pre(Node head) {
28+
if (head == null) {
29+
return;
30+
}
31+
System.out.println(head.value);
32+
pre(head.left);
33+
pre(head.right);
34+
}
35+
36+
public static void in(Node head) {
37+
if (head == null) {
38+
return;
39+
}
40+
in(head.left);
41+
System.out.println(head.value);
42+
in(head.right);
43+
}
44+
45+
public static void pos(Node head) {
46+
if (head == null) {
47+
return;
48+
}
49+
pos(head.left);
50+
pos(head.right);
51+
System.out.println(head.value);
52+
}
53+
54+
public static void main(String[] args) {
55+
Node head = new Node(1);
56+
head.left = new Node(2);
57+
head.right = new Node(3);
58+
head.left.left = new Node(4);
59+
head.left.right = new Node(5);
60+
head.right.left = new Node(6);
61+
head.right.right = new Node(7);
62+
63+
pre(head);
64+
System.out.println("========");
65+
in(head);
66+
System.out.println("========");
67+
pos(head);
68+
System.out.println("========");
69+
70+
}
71+
72+
}

0 commit comments

Comments
 (0)