chenye-814 / DTSTRCT-ALGRTHM

0 stars 0 forks source link

Sep-02 Count Good Nodes in Binary Tree #2

Open chenye-814 opened 2 years ago

chenye-814 commented 2 years ago

https://leetcode.com/problems/count-good-nodes-in-binary-tree/

chenye-814 commented 2 years ago
view code ```py class Solution: def goodNodes(self, root: TreeNode) -> int: def traversal(node: TreeNode, maxNum: int) -> int: if node is None: return 0 curr = 1 if maxNum <= node.val else 0 maxNum = max(maxNum, node.val) return curr + traversal(node.left, maxNum) + traversal(node.right, maxNum) return traversal(root, -10001) ```