Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Node LCA(Node root, int n1, int n2) {
- if (root == null)
- return null;
- if (root.data == n1 || root.data == n2)
- return root;
- Node left_LCA = LCA(root.left, n1, n2);
- Node right_LCA = LCA(root.right, n1, n2);
- if (left_LCA != null && right_LCA != null) {
- return root; // Possible LCA
- }
- return (left_LCA != null) ? left_LCA : right_LCA;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement