Explicit Python solution


  • 0
    P

    class Solution(object):
    def findLeaves(self, root):
    if root is None:
    return []
    left_leaves = self.findLeaves(root.left)
    right_leaves = self.findLeaves(root.right)
    h_left = len(left_leaves)
    h_right = len(right_leaves)
    if h_left < h_right:
    h_low = h_left
    h_high = h_right
    else:
    h_low = h_right
    h_high = h_left
    leaves = []
    for i in range(0, h_low):
    leaves.append(left_leaves[i] + right_leaves[i])
    for i in range(h_low, h_high):
    if h_left < h_right:
    leaves.append(right_leaves[i])
    else:
    leaves.append(left_leaves[i])
    leaves.append([root.val])
    return leaves


Log in to reply
 

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