DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,864 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
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)

"I made 10x faster JSON.stringify() functions, even type safe"

☝️ Must read for JS devs