本文主要是介绍小黑和阿黄一起楼道里运动被宿管告状让导员电话骂了一顿十分委屈的leetcode之旅:404. 左叶子之和,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
小黑代码
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:# 根结点为空,则跳出if not root:return 0self.sum = 0# 递归函数def dfs(node):# 递归出口if node.left:if not (node.left.left or node.left.right):self.sum += node.left.valelse: dfs(node.left)if node.right:dfs(node.right)dfs(root)return self.sum
层次遍历
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:# 根结点为空,则跳出if not root:return 0sum_ = 0# 初始化队列q = collections.deque([root])# 声明匿名函数,判断该点是否是叶子结点is_leaf = lambda node:not (node.left or node.right)# 开始进行层次遍历while q:top = q.popleft()if top.left:if is_leaf(top.left):sum_ += top.left.valelse:q.append(top.left)if top.right:q.append(top.right)return sum_
小黑生活
和阿黄的泡面之旅
和阿黄偷偷会面被莫名发现挨批了,这几天打算消停一下了
接到了辅导员的电话,被狠狠骂了一顿
准备继续学习计算机网络
这篇关于小黑和阿黄一起楼道里运动被宿管告状让导员电话骂了一顿十分委屈的leetcode之旅:404. 左叶子之和的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!