isSameBinaryTree
// Given two binary trees, write a function to check if they are equal or not.
// Two binary trees are considered equal if they are structurally identical
// and the nodes have the same value.
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} left
* @param {TreeNode} right
* @return {boolean}
*/
export default function isSameBinaryTree(left, right) {
// If both is null
if(left === null && right=== null) {
return true;
}
// one null, other is not null, false
if(left !== null && right === null || left === null && right !== null) {
return false;
}
// val diff, false
if(left.val != right.val) {
return false;
}
// find next level of tree (left and right)
return isSameBinaryTree(left.right, right.right) && isSameBinaryTree(left.left, right.left);
};