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.
I had the same question before.
Let's look at the tree first:
4 null 5 null
Then count the "tilt" from bottom:
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!
@Andyyoung Thanks Andy.That made it clear.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.