Why is the output of "{1, 2}, 1" is false?

  • 14

    2 *

    The struct is like above, and the sum of right path is 1. Why the output is false?


    Find another problem: the output of "{1}, 1" is true......


  • 9

    the question is "determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum". So the condition is there is a path from the root to a leaf. For your tree, there is only one leaf (2)

  • 8

    the key is:

    if (node->left == NULL && node -> right == NULL)
        node is leaf;
        node is not leaf;

    so 1 is not leaf

    maybe this example is more clear:

    {1, 2, 3, 4}, 3 is false, just two path: 1 -> 2 -> 4 or 1 -> 3, but 1 -> 2 is not

  • 6

    Definition of leaf: node that has no children.
    That confused me for a while also.

  • 0

    Have the same question. Now, figure out the exactly meaning "the node without children is leaf".

Log in to reply

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