Skip to content

Commit 813dfd5

Browse files
add 872
1 parent d8301d9 commit 813dfd5

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Your ideas/fixes/algorithms are more than welcome!
4242
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java) | O(n) | O(1) | |Easy|
4343
|884|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_884.java) | O(n) | O(k) | |Easy|
4444
|876|[Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_876.java) | O(n) | O(1) | |Easy|
45+
|872|[Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_872.java) | O(n) | O(h) | |Easy| DFS, recursion
4546
|868|[Binary Gap](https://leetcode.com/problems/binary-gap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_868.java) | O(n) | O(n) | |Easy|
4647
|867|[Transpose Matrix](https://leetcode.com/problems/transpose-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_867.java) | O(r*c) | O(r*c) | |Easy|
4748
|859|[Buddy Strings](https://leetcode.com/problems/buddy-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_859.java) | O(n) | O(n) | |Easy|
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* 872. Leaf-Similar Trees
9+
*
10+
* Consider all the leaves of a binary tree. From left to right order, the values of those leaves form a leaf value sequence.
11+
*
12+
* 3
13+
* / \
14+
* 5 1
15+
* / \ / \
16+
* 6 2 9 8
17+
* / \
18+
* 7 4
19+
*
20+
* For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).
21+
*
22+
* Two binary trees are considered leaf-similar if their leaf value sequence is the same.
23+
*
24+
* Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.
25+
*
26+
* Note:
27+
*
28+
* Both of the given trees will have between 1 and 100 nodes.
29+
*/
30+
public class _872 {
31+
public static class Solution1 {
32+
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
33+
List<Integer> leaves1 = new ArrayList<>();
34+
List<Integer> leaves2 = new ArrayList<>();
35+
preorder(root1, leaves1);
36+
preorder(root2, leaves2);
37+
return leaves1.equals(leaves2);
38+
}
39+
40+
private void preorder(TreeNode root,
41+
List<Integer> leaves) {
42+
if (root == null) {
43+
return;
44+
}
45+
if (root.left == null && root.right == null) {
46+
leaves.add(root.val);
47+
}
48+
preorder(root.left, leaves);
49+
preorder(root.right, leaves);
50+
}
51+
}
52+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._872;
6+
import java.util.Arrays;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _872Test {
13+
private static _872.Solution1 solution1;
14+
private static TreeNode root1;
15+
private static TreeNode root2;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _872.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
root1 = TreeUtils.constructBinaryTree(Arrays.asList(3, 5, 6, 2, 7, 4, 1, 9, 8));
25+
root2 = TreeUtils.constructBinaryTree(Arrays.asList(3, 5, 6, 2, 7, 4, 1, 9, 8));
26+
TreeUtils.printBinaryTree(root1);
27+
TreeUtils.printBinaryTree(root2);
28+
assertEquals(true, solution1.leafSimilar(root1, root2));
29+
}
30+
31+
@Test
32+
public void test2() {
33+
root1 =
34+
TreeUtils.constructBinaryTree(Arrays.asList(18, 35, 22, null, 103, 43, 101, 58, null, 97));
35+
TreeUtils.printBinaryTree(root1);
36+
root2 =
37+
TreeUtils.constructBinaryTree(Arrays.asList(94, 102, 17, 122, null, null, 54, 58, 101, 97));
38+
TreeUtils.printBinaryTree(root2);
39+
assertEquals(false, solution1.leafSimilar(root1, root2));
40+
}
41+
}

0 commit comments

Comments
 (0)