public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if( p == null && q == null) {
return true;
}
if(p == null || q == null) {
return false;
}
if( p.val == q.val) {
return isSameTree(p.left,q.left) && isSameTree(p.right, q.right);
}
return false;
}
}
Nice solution! :)
I had a JavaScript solution: https://discuss.leetcode.com/topic/121171/javascript-solution
I like yours better. I converted to JavaScript, and as expected, it worked beautifully.
Mine ran at 128ms and using your solution in JS 96ms – a 33% increase!