Skip to content

Commit 2811d3e

Browse files
author
rchen102
committed
random, intervals
1 parent 3c47c51 commit 2811d3e

26 files changed

+510
-99
lines changed
File renamed without changes.
File renamed without changes.

JZandNC/NC 60.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// 判断是否是完全二叉树
2+
public boolean isCBST(TreeNode root) {
3+
if (root == null) return true;
4+
Queue<TreeNode> queue = new LinkedList<>();
5+
queue.offer(root);
6+
while (!queue.isEmpty()) {
7+
int size = queue.size();
8+
boolean findNULL = false;
9+
for (int i = 0; i < size; i++) {
10+
TreeNode cur = queue.poll();
11+
if (cur == null) {
12+
findNULL = true;
13+
}
14+
else {
15+
if (findNULL) return false;
16+
queue.offer(cur.left);
17+
queue.offer(cur.right);
18+
}
19+
}
20+
}
21+
return true;
22+
}

JZandNC/NC 84.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution {
2+
public int nodeNum(TreeNode head) {
3+
if (head == null) return 0;
4+
int leftH = getHeight(head.left);
5+
int rightH = getHeight(head.right);
6+
if (leftH > rightH) {
7+
return (int)Math.pow(2, rightH) - 1 + 1 + nodeNum(head.left);
8+
}
9+
else {
10+
return (int)Math.pow(2, leftH) - 1 + 1 + nodeNum(head.right);
11+
}
12+
}
13+
14+
public int getHeight(TreeNode cur) {
15+
if (cur == null) return 0;
16+
return 1 + getHeight(cur.left);
17+
}
18+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Java/leetcode 102.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88
* }
99
*/
1010

11-
//Solution1: BFS iterative T: O(n) S: O(n) (node: (n+1) / 2)
11+
// Solution1: BFS iterative T: O(n) S: O(n) (node: (n+1) / 2)
1212
class Solution {
1313
public List<List<Integer>> levelOrder(TreeNode root) {
1414
List<List<Integer>> res = new LinkedList<>();
15+
if (root == null) return res;
1516
Queue<TreeNode> queue = new LinkedList<>();
16-
if(root == null) return res;
1717
queue.offer(root);
18-
while(!queue.isEmpty()) {
19-
int len = queue.size();
20-
List<Integer> tmp = new LinkedList<>();
21-
for(int i = 0; i < len; i++) {
18+
while (!queue.isEmpty()) {
19+
int size = queue.size();
20+
List<Integer> level = new LinkedList<>();
21+
for (int i = 0; i < size; i++) {
2222
TreeNode cur = queue.poll();
23-
tmp.add(cur.val);
24-
if(cur.left != null) queue.offer(cur.left);
25-
if(cur.right != null) queue.offer(cur.right);
23+
level.add(cur.val);
24+
if (cur.left != null) queue.offer(cur.left);
25+
if (cur.right != null) queue.offer(cur.right);
2626
}
27-
res.add(tmp);
27+
res.add(level);
2828
}
2929
return res;
3030
}

Java/leetcode 124.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Solution: post-order recursive
2+
class Solution {
3+
4+
int res;
5+
6+
public int maxPathSum(TreeNode root) {
7+
if (root == null) return 0;
8+
res = root.val;
9+
maxPathEndWith(root);
10+
return res;
11+
}
12+
13+
int maxPathEndWith(TreeNode cur) {
14+
if (cur == null) return 0;
15+
int left = Math.max(0, maxPathEndWith(cur.left));
16+
int right = Math.max(0, maxPathEndWith(cur.right));
17+
int maxPath = left + right + cur.val;
18+
res = Math.max(res, maxPath);
19+
return Math.max(left, right) + cur.val;
20+
}
21+
}
22+
23+
24+
// Solution: post-order iterative
25+
class Solution {
26+
public int maxPathSum(TreeNode root) {
27+
if (root == null) return 0;
28+
Stack<TreeNode> stack = new Stack<>();
29+
HashMap<TreeNode, Integer> map = new HashMap<>();
30+
HashMap<TreeNode, Integer> pathEnd = new HashMap<>();
31+
stack.push(root);
32+
map.put(root, 1);
33+
int res = root.val;
34+
while (!stack.isEmpty()) {
35+
TreeNode cur = stack.pop();
36+
if (map.get(cur) == 0) {
37+
int left = cur.left == null ? 0 : Math.max(0, pathEnd.get(cur.left));
38+
int right = cur.right == null ? 0 : Math.max(0, pathEnd.get(cur.right));
39+
res = Math.max(res, left + right + cur.val);
40+
pathEnd.put(cur, Math.max(left, right) + cur.val);
41+
}
42+
else {
43+
stack.push(cur);
44+
map.put(cur, 0);
45+
if (cur.right != null) {
46+
stack.push(cur.right);
47+
map.put(cur.right, 1);
48+
}
49+
if (cur.left != null) {
50+
stack.push(cur.left);
51+
map.put(cur.left, 1);
52+
}
53+
}
54+
}
55+
return res;
56+
}
57+
}

0 commit comments

Comments
 (0)