-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13-二叉树的比较.js
More file actions
41 lines (38 loc) · 1 KB
/
13-二叉树的比较.js
File metadata and controls
41 lines (38 loc) · 1 KB
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
function Node (value) {
this.value = value;
this.left = null;
this.right = null;
}
let a1 = new Node('a');
let b1 = new Node('b');
let c1 = new Node('c');
let d1 = new Node('d');
let e1 = new Node('e');
let f1 = new Node('f');
let g1 = new Node('g');
a1.left = b1;
a1.right = c1;
b1.left = d1;
b1.right = e1;
c1.left = f1;
c1.right = g1;
let a2 = new Node('a');
let b2 = new Node('b');
let c2 = new Node('c');
let d2 = new Node('d');
let e2 = new Node('e');
let f2 = new Node('f');
let g2 = new Node('g');
a2.left = b2;
a2.right = c2;
b2.left = d2;
b2.right = e2;
c2.left = f2;
c2.right = g2;
function compareTree (root1,root2) {
if(root1 == root2) return true;
if(root1 == null && root2 != null || root1 != null && root2 == null) return false;
if(root1.value != root2.value) return false;
return compareTree(root1.left, root2.left) && compareTree(root1.right,root2.right) || compareTree(root1.left, root2.right) && compareTree(root1.right,root2.left);
}
console.log(compareTree(a1,a2));