对称二叉树
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [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
题解:
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isSymmetric = function(root) {
if(!root) return true
const isMirror = function(tree1,tree2){
if(!tree1&&!tree2){
return true
}
if( tree1 && tree2 &&
tree1.val === tree2.val &&
isMirror(tree1.left,tree2.right) &&
isMirror(tree1.right,tree2.left)
){
return true
}
return false
}
return isMirror(root.left,root.right)
}