A suspected problem


  • 0
    D

    First of all, my English isn't very good and it will be honor for me that you can understand me.
    I'm a college student and I began to use LeetCode to improve my programming skills since last week. 10 minutes ago, I found something which may be inappropriate in Problem 235, Lowest Common Ancestor of a Binary Search Tree.

    According to the question, it's a LCA problem in a BST and we can use a characteristic of BST, that is to say, orderly traversal. Then we can get the code:

    class Solution {
    public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
    int num1 = p->val,num2 = q->val;
    if(num1 > num2)
    swap(num1,num2);
    if(root == NULL)
    return NULL;
    else
    {
    int num = root->val;
    if(num > num2)
    return lowestCommonAncestor(root->left,p,q);
    else if(num < num1)
    return lowestCommonAncestor(root->right,p,q);
    else
    return root;
    }
    }
    };

    And it was accepted. However, it will get improper answer on this occasion:
    0_1506261122853_ca434477-0a30-46ff-93e8-2a071916b176-image.png
    On this occasion, given node is p and q, the proper answer is node r but the answer we will get is node s. Of course, the value of two nodes is the same, but the addresses of two nodes is different.
    What's more, we can get proper answer by DFS, but we will lose the characteristic of BST if we do that. In my opinion, the proper return value should be integer, the value of the treenode.
    My suggestion is above and I'm waiting for your echo and thought about my suggestion. Thank you.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.