DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Is Symmetric Tree

/**
 * 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}
 */
var isSymmetric = function(root) {
    if(root===null) return true;
    return isSym(root.left, root.right)
};

function isSym(left, right) {
    if(left === null || right === null) return left === right
    if(left.val !== right.val) return false
    return isSym(left.left, right.right) && isSym(right.left, left.right)
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)