Appearance
对称二叉树
js
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
function isSame(n1, n2) {
if (n1 && n2) {
return (
n1.val === n2.val &&
isSame(n1.left, n2.right) &&
isSame(n1.right, n2.left)
);
} else {
return n1 == null && n2 == null;
}
}
var isSymmetric = function (root) {
return isSame(root.left, root.right);
};
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
function isSame(n1, n2) {
if (n1 && n2) {
return (
n1.val === n2.val &&
isSame(n1.left, n2.right) &&
isSame(n1.right, n2.left)
);
} else {
return n1 == null && n2 == null;
}
}
var isSymmetric = function (root) {
return isSame(root.left, root.right);
};