-
Notifications
You must be signed in to change notification settings - Fork 5
/
LeafSimilarTrees.java
43 lines (39 loc) · 1.51 KB
/
LeafSimilarTrees.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
Problem Statement:
Consider all the leaves of a binary tree. From left to right order, the values of those leaves form a leaf value sequence.
Problem Link:
Leaf Similar Trees: https://leetcode.com/problems/leaf-similar-trees/description/
Solution:
https://github.com/sunnypatel165/leetcode-again/blob/master/solutions/FizzBuzz.java
Author:
Sunny Patel
https://github.com/sunnypatel165
https://www.linkedin.com/in/sunnypatel165/
*/
class Solution {
List<Integer> leafValueSequence1 = new ArrayList<>();
List<Integer> leafValueSequence2 = new ArrayList<>();
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
getLeafValueSequence(root1, leafValueSequence1);
getLeafValueSequence(root2, leafValueSequence2);
if(leafValueSequence2.size()!=leafValueSequence1.size())
return false;
for(int i=0;i<leafValueSequence2.size();i++)
if(leafValueSequence1.get(i)!=leafValueSequence2.get(i))
return false;
return true;
}
public void getLeafValueSequence(TreeNode root, List<Integer> leafValueSequence){
if(root==null)
return;
if(root.left==null && root.right==null){
leafValueSequence.add(root.val);
return;
}
if(root.left!=null)
getLeafValueSequence(root.left, leafValueSequence);
if(root.right!=null)
getLeafValueSequence(root.right, leafValueSequence);
}
}