如下图
const Node = require("../common")
// 二叉树1
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 = c1;
c1.left = f1;
b1.left = d1;
a1.right = b1;
b1.right = e1;
c1.right = g1;
// 二叉树2
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.right = c2;
c2.left = f2;
b2.left = d2;
a2.left = b2;
b2.right = e2;
c2.right = g2;
/**
* 二叉树左右子树交换的比较
* @param {*} root1 二叉树1
* @param {*} root2 二叉树2
*/
function comparisonExchangeTree(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 comparisonExchangeTree(root1.left, root2.left) && comparisonExchangeTree(root1.right, root2.right) || comparisonExchangeTree(root1.left, root2.right) && comparisonExchangeTree(root1.right, root2.left)
}
console.log(comparisonExchangeTree(a1,a2))
导入的common.js
module.exports = class Node{
constructor(value){
this.value = value;
this.left = null;
this.right = null;
}
}