-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path872.dart
44 lines (35 loc) · 1.02 KB
/
872.dart
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
44
// Leaf-Similar Trees
// 각각의 집안에서 제일 어린 자식노드들이 서로 같은 값으로, 같은 순서로 배열돼 있는지 비교하기
/**
* Definition for a binary tree node.
* class TreeNode {
* int val;
* TreeNode? left;
* TreeNode? right;
* TreeNode([this.val = 0, this.left, this.right]);
* }
*/
class Solution {
bool leafSimilar(TreeNode? root1, TreeNode? root2) {
List<int> kids1 = [];
List<int> kids2 = [];
void getKids(TreeNode? currNode, List<int> kids) {
if (currNode == null) return ;
if (currNode.left==null && currNode.right==null){
kids.add(currNode.val);
}
getKids(currNode.left, kids);
getKids(currNode.right, kids);
} // fn
getKids(root1, kids1);
getKids(root2, kids2);
if (kids1.length == kids2.length) {
for (var i = 0; i<kids1.length; i++) {
if (kids1[i] != kids2[i]){
return false;
}
} return true;
}
return false;
}
}