class Solution:
def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
result = []
path = []
if not root:
return result
else:
self.getPath(root,path,result)
return result
def getPath(self,cur,path,result):
path.append(str(cur.val))
if not cur.left and not cur.right:
result.append('->'.join(path))
return
if cur.left:
self.getPath(cur.left, path, result)
path.pop()
if cur.right:
self.getPath(cur.right,path, result)
path.pop()
class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
if root.left is None and root.right is None:
return 0
leftValue = self.sumOfLeftLeaves(root.left) # 左
if root.left and not root.left.left and not root.left.right: # 左子树是左叶子的情况
leftValue = root.left.val
rightValue = self.sumOfLeftLeaves(root.right) # 右
sum_val = leftValue + rightValue # 中
return sum_val