File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change 234
234
| 860 | [ Lemonade Change] ( https://leetcode.com/problems/lemonade-change ) | [ ![ Java] ( assets/java.png )] ( src/LemonadeChange.java ) |
235
235
| 867 | [ Transpose Matrix] ( https://leetcode.com/problems/transpose-matrix ) | [ ![ Java] ( assets/java.png )] ( src/TransposeMatrix.java ) |
236
236
| 868 | [ Binary Gap] ( https://leetcode.com/problems/binary-gap ) | [ ![ Java] ( assets/java.png )] ( src/BinaryGap.java ) |
237
- | 872 | [ Leaf-Similar Trees] ( https://leetcode.com/problems/leaf-similar-trees ) | |
237
+ | 872 | [ Leaf-Similar Trees] ( https://leetcode.com/problems/leaf-similar-trees ) | [ ![ Java ] ( assets/java.png )] ( src/LeafSimilarTrees.java ) |
238
238
| 874 | [ Walking Robot Simulation] ( https://leetcode.com/problems/walking-robot-simulation ) | |
239
239
| 876 | [ Middle of the Linked List] ( https://leetcode.com/problems/middle-of-the-linked-list ) | |
240
240
| 883 | [ Projection Area of 3D Shapes] ( https://leetcode.com/problems/projection-area-of-3d-shapes ) | |
Original file line number Diff line number Diff line change
1
+ import java .util .ArrayList ;
2
+ import java .util .List ;
3
+
4
+ public class LeafSimilarTrees {
5
+ public boolean leafSimilar (TreeNode root1 , TreeNode root2 ) {
6
+ List <Integer > leafSequence1 = getLeafSequence (root1 );
7
+ List <Integer > leafSequence2 = getLeafSequence (root2 );
8
+ return leafSequence1 .equals (leafSequence2 );
9
+ }
10
+
11
+ private List <Integer > getLeafSequence (TreeNode root ) {
12
+ List <Integer > result = new ArrayList <>();
13
+ getLeafSequence (root , result );
14
+ return result ;
15
+ }
16
+
17
+ private void getLeafSequence (TreeNode root , List <Integer > leafNodes ) {
18
+ if (root == null ) return ;
19
+ if (root .left == null && root .right == null ) {
20
+ leafNodes .add (root .val );
21
+ return ;
22
+ }
23
+ getLeafSequence (root .left , leafNodes );
24
+ getLeafSequence (root .right , leafNodes );
25
+ }
26
+ }
You can’t perform that action at this time.
0 commit comments