Problem Solving/LeetCode
Today's Challenge
fabichoi
2022. 9. 1. 23:45
https://leetcode.com/problems/count-good-nodes-in-binary-tree/
Count Good Nodes in Binary Tree - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
3일 연속 DFS 문제 풀이
class Solution:
def goodNodes(self, root: TreeNode) -> int:
if not root:
return 0
def dfs(node, cur_max):
if not node:
return
if node.val >= cur_max:
count[0] += 1
cur_max = node.val
dfs(node.left, cur_max)
dfs(node.right, cur_max)
count = [0]
dfs(root, root.val)
return count[0]
반응형