当一棵树的左右子树互换时不算同一棵树
function Node(value) {
this.value = value;
this.left = null;
this.right = null;
}
var a1 = new Node("a");
var b1 = new Node("b");
var c1 = new Node("c");
var d1 = new Node("d");
var e1 = new Node("e");
var f1 = new Node("f");
var g1 = new Node("g");
a1.left = c1;
a1.right = b1;
c1.left = f1;
c1.right = g1;
b1.left = d1;
b1.right = e1;
var a2 = new Node("a");
var b2 = new Node("b");
var c2 = new Node("c");
var d2 = new Node("d");
var e2 = new Node("e");
var f2 = new Node("f");
var g2 = new Node("g");
a2.left = c2;
a2.right = b2;
c2.left = f2;
c2.right = g2;
b2.left = d2;
b2.right = e2;
function compareTree(root1, root2) {
if (root1 == root2) return true;
if (root1 == null && root2 != null || root2 == null && root != null) return false;
if (root1.value != root2.value) return false;
var leftBool = compareTree(root1.left,root2.left);
var rightBool = compareTree(root1.right,root2.right);
return leftBool && rightBool;
}
console.log(compareTree(a1, a2))
当一棵树的左右子树互换时算同一棵树
function Node(value) {
this.value = value;
this.left = null;
this.right = null;
}
var a1 = new Node("a");
var b1 = new Node("b");
var c1 = new Node("c");
var d1 = new Node("d");
var e1 = new Node("e");
var f1 = new Node("f");
var g1 = new Node("g");
a1.left = c1;
a1.right = b1;
c1.left = f1;
c1.right = g1;
b1.left = d1;
b1.right = e1;
var a2 = new Node("a");
var b2 = new Node("b");
var c2 = new Node("c");
var d2 = new Node("d");
var e2 = new Node("e");
var f2 = new Node("f");
var g2 = new Node("g");
a2.right = c2;
a2.left = b2;
c2.left = f2;
c2.right = g2;
b2.left = d2;
b2.right = e2;
function compareTree(root1, root2) {
if (root1 == root2) return true;
if (root1 == null && root2 != null || root2 == null && root != null) return false;
if (root1.value != root2.value) return false;
var leftBool = compareTree(root1.left,root2.left);
var rightBool = compareTree(root1.right,root2.right);
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))