Skip to content

Commit

Permalink
add Easy_100_Same_Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
zongyanqi committed Mar 19, 2017
1 parent 984e54f commit d756fe9
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Easy_100_Same_Tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function (p, q) {

if (!p && !q) return true;
if (!p || !q) return false;
if (p.val !== q.val) return false;

return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);

};


0 comments on commit d756fe9

Please sign in to comment.