两数之和

 

 

递归:

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

};
function TreeJug(left, right) {
    if (!left && left === right) {
        return true;
    }
    if (!left || !right) {
        return false;
    }
    if (left.val !== right.val) {
        return false;
    }
    return TreeJug(left.left, right.right) && TreeJug(left.right, right.left)
}

迭代:利用队列

  

var isSymmetric = function(root) {
    if(root)  return true
    const arr = []
    arr.push(root.left)
    arr.push(root.right)
    while(arr.length !=0 ){
        let node_left = arr.shift()
        let node_right = arr.shift()
        if(node_left === null && node_right === null)   continue
        if(node_left === null || node_right === null || node_right.val != node_left.val)   return false
        arr.push(node_left.left)
        arr.push(node_right.right)
        arr.push(node_left.right)
        arr.push(node_right.left)
    }
};

  

 

posted @ 2021-03-01 18:54  Jiox  阅读(96)  评论(0)    收藏  举报