Why can't I use root.val == p.val for comparison?

  • 3

    my code is pretty much the same as everyone else's, but I changed the comparison

    if (root == p || root == q) then return root;


    if (root.val == p.val || root.val == q.val) then return root;

    then I got errors, I'm really confused, if the former conditions are satisfied, the latter must be true too.

  • 2

    because there are nodes with the same values.

  • 0

    yeah. it is weird though because the question makes more sense to me if the method is taking a root node, and 2 int values as parameters instead of 2 other TreeNodes.

    Then in the case there is duplicated values, it should return the lowest node that has the two values as descendents.

Log in to reply

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