파비의 매일매일 공부기록

Today's Challenge 본문

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]
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.09.03
Today's Challenge  (0) 2022.09.02
Today's Challenge  (0) 2022.08.31
Today's Challenge  (0) 2022.08.30
Today's Challenge  (0) 2022.08.29
Comments