// 101. Symmetric Tree
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isSymmetric = function(root) {
/*start from root*/
if(root === null || (root.right === null && root.left === null)){
return true
}
/*revertTree*/
function revertTree(node){
if(node === null || (node.left === null && node.right === null)){
return node
}
let temp = revertTree(node.left)
node.left = revertTree(node.right)
node.right = temp
return node
}
/*compare isSame?*/
function isSameTree(left,right){
if(left === null && right === null){
return true
}
if((left === null && right !== null) ||(right === null && left !== null)){
return false
}
if(left.val !== right.val){
return false
}
return isSameTree(left.right,right.right) && isSameTree(left.left,right.left)
}
/*revert right tree to left and compare them whether they are the same.*/
root.right = revertTree(root.right)
return isSameTree(root.left,root.right)
};