题目描述:
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/ \
2 2
/ \ / \
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/ \
2 2
\ \
3 3
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/symmetric-tree
var isSymmetric = function(root) {
// 采用中序遍历的方法进行递归判断
if(root === null) return true;
return symmecticTree(root.left, root.right);
};
function symmecticTree(left, right) {
if(left === null || right === null) {
return left === right;
}
if(left.val !== right.val) {
return false;
}
return symmecticTree(left.left,right.right) && symmecticTree(left.right,right.left);
}