# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def Sum(self, root):
"""
:type root: TreeNode
:type sum: int
:rtype: int
"""
if not root:
return 0
else:
return root.val+self.Sum(root.left)+self.Sum(root.right)
猜你喜欢:👇🏻
⭐【Leetcode】二叉树每个路径的节点之和
⭐【Leetcode】输出二叉树的所有路径(数值型)
⭐【Leetcode】Python 代码本地构造二叉树、链表