leetcode icon indicating copy to clipboard operation
leetcode copied to clipboard

Bug Report for lowest-common-ancestor-in-binary-search-tree

Open suryatejess opened this issue 2 months ago • 0 comments

Bug Report for https://neetcode.io/problems/lowest-common-ancestor-in-binary-search-tree

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null || root == q || root == p){
            return root; 
        }        

        TreeNode left = lowestCommonAncestor(root.left, p, q); 
        TreeNode right = lowestCommonAncestor(root.right, p, q); 

        if(left == null){
            return right; 
        }
        else if(right == null){
            return left; 
        }
        else{
            return root; 
        }
    }
}

this is my code, and I am getting a null pointer exception for this. I believe there must be some problem with the way the output is being rendered and hence the NPA

suryatejess avatar Oct 08 '25 05:10 suryatejess