From 593065e40a8a60129f9827c27521aef6d0ae9999 Mon Sep 17 00:00:00 2001 From: Ayush Jain Date: Thu, 7 May 2020 03:00:20 +0530 Subject: [PATCH] Handling Extra Case For tree @ https://assets.leetcode.com/uploads/2018/12/14/binarytree.png This code fails, and returns null where the actual lca is 5 --- java/Chapter 4/Question4_7/QuestionB.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/Chapter 4/Question4_7/QuestionB.java b/java/Chapter 4/Question4_7/QuestionB.java index fe4854aa..7b741113 100644 --- a/java/Chapter 4/Question4_7/QuestionB.java +++ b/java/Chapter 4/Question4_7/QuestionB.java @@ -15,7 +15,7 @@ public static TreeNode commonAncestorHelper(TreeNode root, TreeNode p, TreeNode } boolean is_p_on_left = covers(root.left, p); boolean is_q_on_left = covers(root.left, q); - if (is_p_on_left != is_q_on_left) { // Nodes are on different side + if (is_p_on_left != is_q_on_left || root == p || root == q) { // Nodes are on different side return root; } TreeNode child_side = is_p_on_left ? root.left : root.right;