I'm trying to return the length of all the paths and the actual paths of a binary tree with the values ordered by the position of the leaf nodes from left to right.
For eg, for this binary tree (picture below), the answer should be [(3, '4‐2‐1'), (4, '8‐5‐2‐1'), (4, '9‐5‐2‐1'), (2, '7‐1')] because the paths to leaf nodes are "1‐2‐4" (length 3), "1‐2‐5‐8" (length 4) "1‐2‐ 5‐9" (length 4) etc.
I've tried using recursion (sample code below) to count the length of the path but keep getting a TypeError: unsupported operand type(s) for +: 'int' and 'list'.
res = []
count = 0
if T== None:
return 0
lengthL = 1 + self._pathV2(T.leftT)
lengthR = 1+ self._pathV2(T.rightT)
count = lengthR + lengthL
res.append(count)
Any help with solving this problem will be much appreciated!
