Created
January 6, 2015 02:09
-
-
Save rioshen/7cb5bcf81e1c6045a15d to your computer and use it in GitHub Desktop.
Find Intersection of Two BST
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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