欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

LeetCode力扣 101. 对称二叉树 Symmetric Tree 题解代码 JavaScript

程序员文章站 2022-05-16 14:56:04
...

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}
 */
function isTreeFun(left, right) {
    if(!left && !right)
        return true;
    if(!left || !right)
        return false;
    if(left.val != right.val)
        return false;
    return isTreeFun(left.left, right.right) && isTreeFun(left.right, right.left);
}

var isSymmetric = function(root) {
    if(!root)
        return true;
    return isTreeFun(root.left, root.right);
};