Binary tilt tree: Explain the given test case.


  • 1
    R

    Link to problem: https://leetcode.com/problems/binary-tree-tilt/#/description

    Test case to explain: [1,2,3,4,null,5]
    Expected O/P for test case: 11
    My O/P: 10

    Kindly explain the approach arithmetically and program logic is not required.


  • 5
    A

    I had the same question before.

    Let's look at the tree first:
    1
    2 3
    4 null 5 null

    Then count the "tilt" from bottom:
    4->0,
    5->0,
    2->4,
    3->5,

    node 1:
    left=2+4=6
    right=3+5=8
    so tilt of node 1=8-6=2

    the tilt of the tree=0+0+4+5+2=11

    I believe you thought the tilt of 1 should be 3-2=1,which means the difference between its left node and right code, but it is actually the difference between the sum of all nodes' value of its left subtree and sum of right subtree.
    I had the same thought as you before, hope it helps!


  • 0
    R

    @Andyyoung Thanks Andy.That made it clear.


  • 0
    A

    Thanks Andy, I have the same problem too and now I understood. :)


Log in to reply
 

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