The question needs to be re-defined.

  • 20

    The weight increases from the leaf to the root.

    However, the following situation is not clearly defined. I will illustrate it using a tree structure.

      /    \
     b      c

    What is the weight of a? Is it 2 to 3?

  • 1

    its 3. We need find maxDepth first before we can start calculating sum

  • 31

    What I found more surprising is that b, which is a leaf, has weight 2. Even though we're told that "the leaf level integers have weight 1".

  • 0

    In my opinion from what the question says, the depth of a is 2 for left child and 3 for right child.

  • 1

    It should be defined as "the deepest leaf level integers have weight 1"

  • 2

    This question can easily mislead readers. This is not how we generally define the "depth" of a node in the tree. If I remember correctly, "Depth" means the length from root to a node and "height" means the length from the node to its deepest child.

    Apparently, in this question, neither of them is our case. I think the question should emphasize their special definition "depth", to avoid misunderstand.

  • 0

    I have tried this test case: [1,[3,[4]], [2,100]] and the expected result is 217, I think this question only consider all subtrees has the same length.

Log in to reply

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