从根节点开始检测二叉树
- 左右节点是否都为空
- 是否其中一个为空
- 是否两个都不为空,但左右节点值不等
- 是否两个都不为空,但左右节点值相等,递归调用
function Tree(pTree) {
if (!pTree) {
return true;
}
return TreeJug(pTree.left, pTree.right);
}
function TreeJug(left, right) {
if (!left && left === right) {
return true;
}
if (!left || !right) {
return false;
}
if (left.val !== right.val) {
return false;
}
return TreeJug(left.left, right.right) && TreeJug(left.right, right.left);
}
let tree = {
val: 1,
left: null,
right: null
};
console.log(Tree(tree));