Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question 4.7 has a bug. If one TreeNode is under the other one you get a NullPointerException #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions java/Chapter 4/Question4_7/QuestionBFixed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

import CtCILibrary.TreeNode;

public class QuestionBFixed {
public static boolean covers(TreeNode root, TreeNode p) {
if (root == null) return false;
if (root == p) return true;
return covers(root.left, p) || covers(root.right, p);
}

public static TreeNode commonAncestorHelper(TreeNode root, TreeNode p, TreeNode q,
boolean covered) {
if (root == null) {
return null;
}
boolean is_p_on_left = covers(root.left, p);
boolean is_q_on_left = covers(root.left, q);
if(covered && (is_p_on_left || is_q_on_left)) {
return root;
}
if (is_p_on_left != is_q_on_left) { // Nodes are on different side
return root;
}
TreeNode child_side = is_p_on_left ? root.left : root.right;
return commonAncestorHelper(child_side, p, q, covered);
}

public static TreeNode commonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (!covers(root, p) || !covers(root, q)) { // Error check - one node is not in tree
return null;
}
boolean covered = false;
if(covers(p, q) || covers(p, q)) {
covered = true;
}
return commonAncestorHelper(root, p, q, covered);
}

public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
TreeNode root = TreeNode.createMinimalBST(array);
TreeNode n3 = root.find(6);
TreeNode n7 = root.find(7);
TreeNode ancestor = commonAncestor(root, n3, n7);
System.out.println(ancestor.data);
}

}