Skip to content

Instantly share code, notes, and snippets.

@rioshen
Created January 6, 2015 02:09
Show Gist options
  • Save rioshen/7cb5bcf81e1c6045a15d to your computer and use it in GitHub Desktop.
Save rioshen/7cb5bcf81e1c6045a15d to your computer and use it in GitHub Desktop.
Find Intersection of Two BST
class TreeNode {
int val;
TreeNode left;
TreeNode right;
public TreeNode(int x) { this.val = x; }
}
public void printIntersection(TreeNode p, TreeNode q) {
if (p == null || q == null) {
return;
}
if (p.val < q.val) {
printIntersection(p, q.left);
printIntersection(p.right, q);
} else if (p.val > q.val) {
printIntersection(p.left, q);
printIntersection(p, q.right);
} else {
System.out.println("find " + p.val);
printIntersection(p.left, q.left);
printIntersection(p.right, q.right);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment