Skip to content

Instantly share code, notes, and snippets.

@InterviewBytes
Created June 10, 2017 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InterviewBytes/c795a5a60f73e3bf3a4edc2f3c5ed641 to your computer and use it in GitHub Desktop.
Save InterviewBytes/c795a5a60f73e3bf3a4edc2f3c5ed641 to your computer and use it in GitHub Desktop.
Same Tree
package com.interviewbytes.trees;
public class SameTree {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
if (p == null || q == null) return false;
return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
package com.interviewbytes.trees;
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment