两数之和

递归:
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)
}
};

浙公网安备 33010602011771号