数据结构:树🌲
时间复杂度:O(n)
空间复杂度:O(n)
代码实现:
class Solution:
def goodNodes(self, root: TreeNode) -> int:
counter = [0]
def dfs(root, val):
if not root: return
next_val = val
if root.val >= val:
counter[0] += 1
next_val = root.val
dfs(root.left, next_val)
dfs(root.right, next_val)
dfs(root, -10e9)
return counter[0]
本文介绍了一个名为`goodNodes`的树遍历算法,用于计算大于等于给定值的节点数,采用深度优先搜索(DFS)策略。该方法的时间复杂度为O(n),空间复杂度也为O(n)。
349

被折叠的 条评论
为什么被折叠?



