Advertisement
Guest User

Same Tree

a guest
Sep 20th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1.  
  2. Given two binary trees, write a function to check if they are equal or not.
  3.  
  4. Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
  5.  
  6. --------------------
  7. class Solution {
  8. public boolean isSameTree(TreeNode p, TreeNode q) {
  9. if(p == null && q == null)
  10. return true;
  11. if((p == null && q != null) || (p != null && q == null))
  12. return false;
  13. if(p.val != q.val)
  14. return false;
  15. return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
  16. }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement