-
Notifications
You must be signed in to change notification settings - Fork 0
/
same_tree_test.dart
97 lines (76 loc) · 2.07 KB
/
same_tree_test.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import 'dart:collection';
import 'package:test/test.dart';
bool sameTree(Node? a, Node? b) {
final stackA = DoubleLinkedQueue<Node?>()..add(a);
final stackB = DoubleLinkedQueue<Node?>()..add(b);
while (stackA.isNotEmpty || stackB.isNotEmpty) {
a = stackA.pop();
b = stackB.pop();
if (a == null && b == null) continue;
if (a == null && b != null || a != null && b == null) return false;
if (a!.value != b!.value) return false;
stackA
..push(a.left)
..push(a.right);
stackB
..push(b.left)
..push(b.right);
}
return stackA.isEmpty == stackB.isEmpty;
}
extension MinimalistStack<E> on Queue<E> {
void push(E value) => addLast(value);
E pop() => removeLast();
}
bool sameTreeRecursive(Node? a, Node? b) {
if (a == null && b == null) return true;
if (a != null && b == null || a == null && b != null) return false;
if (a!.value != b!.value) return false;
return sameTreeRecursive(a.left, b.left) &&
sameTreeRecursive(a.right, b.right);
}
// Very simple solution, override the == operator
bool sameTreeOperator(Node a, Node b) => a == b;
class Node {
const Node(this.value, [this.left, this.right]);
final int value;
final Node? left;
final Node? right;
@override
bool operator ==(Object o) =>
o is Node && o.value == value && o.left == left && o.right == right;
@override
int get hashCode => Object.hash(value, left, right);
@override
String toString() => '$Node($value, $left, $right)';
}
void main() {
test('leetcode', () {
expect(
Node(1, Node(2), Node(3)) == Node(1, Node(2), Node(3)),
true,
);
expect(
Node(1, Node(2)) == Node(1, null, Node(2)),
false,
);
expect(
Node(1, Node(2), Node(1)) == Node(1, Node(1), Node(2)),
false,
);
});
test('moarrr tests', () {
expect(
Node(1, Node(2), Node(3, Node(4))) == Node(1, Node(2), Node(3)),
false,
);
expect(
Node(1, Node(2)) == Node(1),
false,
);
expect(
Node(1, Node(2), Node(1)) == Node(1, Node(1), Node(2)),
false,
);
});
}