Skip to content

Commit 8e0c0d7

Browse files
committed
update
1 parent 31ec7aa commit 8e0c0d7

File tree

7 files changed

+83
-63
lines changed

7 files changed

+83
-63
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,12 @@
397397
|695|[Max Area of Island](https://leetcode.com/problems/max-area-of-island/)|[Java](leetcode/solution/src/MaxAreaOfIsland.java)|100||
398398
|703|[Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/)|[Java](leetcode/solution/src/KthLargestElementInAStream.java)|100||
399399
|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/)|[Java](leetcode/solution/src/JewelsAndStones.java)|100||
400+
|814|[Binary Tree Pruning](https://leetcode.com/problems/binary-tree-pruning/)|[Java](leetcode/solution/src/JewelsAndStones.java)|80||
400401
|819|[Most Common Word](https://leetcode.com/problems/most-common-word/)|[Java](leetcode/solution/src/MostCommonWord.java)|85||
401402
|843|[Guess the Word](https://leetcode.com/problems/guess-the-word/)|[Java](leetcode/solution/src/GuessTheWord.java)|90||
402403
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Java](leetcode/solution/src/BackspaceStringCompare.java)|100||
403404
|849|[Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person/)|[Java](leetcode/solution/src/MaximizeDistanceToClosestPerson.java)|80||
404405
|857|[Minimum Cost to Hire K Workers](https://leetcode.com/problems/minimum-cost-to-hire-k-workers/)|[Java](leetcode/solution/src/MinimumCostToHireKWorkers.java)|70||
406+
|872|[Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/)|[Java](leetcode/solution/src/LeafSimilarTrees.java)|80||
405407
|904|[Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/)|[Java](leetcode/solution/src/FruitIntoBaskets.java)|90||
406408
|929|[Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/)|[Java](leetcode/solution/src/UniqueEmailAddresses.java)|90||
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class BinaryTreePruning {
2+
3+
public TreeNode pruneTree(TreeNode root) {
4+
return helper(root) ? root : null;
5+
}
6+
7+
private boolean helper(TreeNode root) {
8+
if (root == null) {
9+
return false;
10+
}
11+
boolean left = helper(root.left);
12+
boolean right = helper(root.right);
13+
if (!left) {
14+
root.left = null;
15+
}
16+
if (!right) {
17+
root.right = null;
18+
}
19+
return root.val == 1 || left || right;
20+
}
21+
}

leetcode/solution/src/BinaryTreeTilt.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ public class BinaryTreeTilt {
22

33
int tilt = 0;
44

5+
/**
6+
* 这题意思是遍历所有结点,对每个节点计算tilt,然后求和
7+
* 对于给定root,其tilt为左子树所有节点的和与右子树所有结点的和的差的绝对值
8+
*/
59
public int findTilt(TreeNode root) {
610
helper(root);
711
return tilt;

leetcode/solution/src/FindModeInBinarySearchTree.java

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -62,51 +62,4 @@ private void inorderTraverse(TreeNode root) {
6262

6363
inorderTraverse(root.right);
6464
}
65-
66-
67-
/**
68-
* 如下是空间为O(l)的写法
69-
*/
70-
int mCurVal;
71-
int mMaxElemSize;
72-
int idx;
73-
int[] modes;
74-
75-
public int[] findMode2(TreeNode root) {
76-
helper(root);
77-
modes = new int[mMaxElemSize];
78-
mCurCount = 0;
79-
helper(root);
80-
return modes;
81-
}
82-
83-
private void helper(TreeNode node) {
84-
if (node == null) {
85-
return;
86-
}
87-
88-
helper(node.left);
89-
90-
if (node.val == mCurVal) {
91-
mCurCount++;
92-
} else {
93-
mCurCount = 1;
94-
mCurVal = node.val;
95-
}
96-
97-
if (modes == null) {
98-
if (mCurCount == mMaxCount) {
99-
mMaxElemSize++;
100-
} else if (mCurCount > mMaxCount) {
101-
mMaxElemSize = 1;
102-
mMaxCount = mCurCount;
103-
}
104-
} else {
105-
if (mCurCount == mMaxCount) {
106-
modes[idx++] = mCurVal;
107-
}
108-
}
109-
110-
helper(node.right);
111-
}
11265
}

leetcode/solution/src/InorderSuccessorInBST.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
3535
if (root == null) {
3636
return null;
3737
}
38-
if (root.val > p.val) {
38+
if (p.val < root.val) {
3939
TreeNode node = inorderSuccessor(root.left, p);
4040
return node != null ? node : root;
4141
} else {
@@ -60,7 +60,7 @@ public TreeNode predecessor(TreeNode root, TreeNode p) {
6060
public TreeNode inorderSuccessor2(TreeNode root, TreeNode p) {
6161
TreeNode res = null;
6262
while (root != null) {
63-
if (root.val > p.val) {
63+
if (p.val < root.val) {
6464
res = root;
6565
root = root.left;
6666
} else {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.util.Stack;
2+
3+
public class LeafSimilarTrees {
4+
5+
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
6+
TreeIterator iterator1 = new TreeIterator(root1);
7+
TreeIterator iterator2 = new TreeIterator(root2);
8+
9+
while (true) {
10+
TreeNode node1 = iterator1.nextLeaf();
11+
TreeNode node2 = iterator2.nextLeaf();
12+
13+
if (node1 == null && node2 == null) {
14+
return true;
15+
}
16+
if (node1 == null || node2 == null) {
17+
return false;
18+
}
19+
if (node1.val != node2.val) {
20+
return false;
21+
}
22+
}
23+
}
24+
25+
public class TreeIterator {
26+
Stack<TreeNode> stack;
27+
TreeNode root;
28+
29+
TreeIterator(TreeNode root) {
30+
this.root = root;
31+
stack = new Stack<>();
32+
}
33+
34+
TreeNode nextLeaf() {
35+
TreeNode ret;
36+
while (!stack.isEmpty() || root != null) {
37+
if (root != null) {
38+
stack.push(root);
39+
ret = root;
40+
root = root.left;
41+
42+
if (ret.left == null && ret.right == null) {
43+
return ret;
44+
}
45+
46+
} else {
47+
root = stack.pop().right;
48+
}
49+
}
50+
return null;
51+
}
52+
}
53+
}

leetcode/src/Main.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,8 @@ public class Main {
66

77
public static class Solution {
88

9-
int tilt = 0;
9+
public TreeNode pruneTree(TreeNode root) {
1010

11-
public int findTilt(TreeNode root) {
12-
helper(root);
13-
return tilt;
14-
}
15-
16-
private int helper(TreeNode root) {
17-
if (root == null) {
18-
return 0;
19-
}
20-
int left = helper(root.left);
21-
int right = helper(root.right);
22-
tilt += Math.abs(left - right);
23-
return left + right + root.val;
2411
}
2512
}
2613

0 commit comments

Comments
 (0)