Created
January 31, 2019 18:35
-
-
Save jianminchen/263e05a50a7e337aff2264125e9b9e9a to your computer and use it in GitHub Desktop.
Mock interview Jan 30, 2019 - 10:00 PM - 11:47 PM - 1:06
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//post order | |
leftMoves = getMoves(left) // 1 | |
rightMoves = getMoves(right) // -1 | |
ownMoves = node.val === 0? -1 : node.val // wrong | |
totalMoves = Math.abs(leftMoves) + Math.abs(rightMoves) // each node will be visted once - //moves from each edge | |
return ownMoves + leftMoves + rightMoves // definition of recursive function | |
Example 4: | |
4 | |
/-2 -1\ | |
0 0 | |
\ -1 | |
0 -1 - should be the coins needed to move - recursive function +, - | |
total moves -> absolution | |
3 0 0 1 | |
1 2 | |
-> coins move -> piggyback -> node value calculation first | |
take two steps: | |
for example: | |
left child - math.abs(child.val - 1) | |
right child - math.abs(child.val - 1) | |
how many coins we need to move | |
determine the value of root node -> ? | |
1 + 1 + new root value = root.val + left.val + right.val | |
new root.val | |
The above steps can be in recursive function - post order traversal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment