Advertisement
aero2146

Same Tree

Jan 11th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.35 KB | None | 0 0
  1. class Solution {
  2.   public boolean isSameTree(TreeNode p, TreeNode q) {
  3.     // p and q are both null
  4.     if (p == null && q == null) return true;
  5.     // one of p and q is null
  6.     if (q == null || p == null) return false;
  7.     if (p.val != q.val) return false;
  8.     return isSameTree(p.right, q.right) &&
  9.             isSameTree(p.left, q.left);
  10.   }
  11. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement