Get wrong answer for a tree with each node having positive value


  • 0
    Z

    For the test case, {5,4,8,11,#,13,4,7,2,#,#,#,1}, all the node's value are positive. My code gives output 55, but the expected result is 48. Any one know why? Thank you

                         5
                   /            \
                 4               8
             /                /          \
          11              13            4
      /     \                               \ 
     7      2                                 1

  • 0
    S

    I just format your tree graph correctly. You can select all the graph of tree, then click {} button, just like format code.


  • 0
    S

    You may misunderstand the ONE PATH, which means from one node to another node (might be the same node).

    If you select all node in this tree, it will at least 2 path to cover all nodes, one is from [7] to [13], the other is from [2] to [1].

    For expected result 48, the path is below:

                         [5]
                   /            \
                 [4]              [ 8]
             /                /          \
          [11]              [13 ]           4
      /     \                               \ 
     [7 ]     2                                 1
    

  • 0
    Z

    Thank you. Your're right.


  • 0
    L

    Frankly I feel finding the max sum of a subtree is much harder...


Log in to reply
 

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