Record problem solution: Lituo 1448. Count the number of good nodes in a binary tree

Given a  root binary tree with root , please return the number of good nodes in the binary tree.

A "good node" X is defined as: among the nodes passing through from the root to the node X, there is no node whose value is greater than the value of X.

Example 1:

Input: root = [3,1,4,3,null,1,5]
 Output: 4
 Explanation: The blue nodes in the figure are good nodes. 
The root node (3) is always a good node. 
Node 4 -> (3,4) is the maximum value in the path. 
Node 5 -> (3,4,5) is the maximum value in the path. 
Node 3 -> (3,1,3) is the maximum value in the path.

Example 2:

Input: root = [3,3,null,4,2]
 Output: 3
 Explanation: Node 2 -> (3, 3, 2) is not a good node because "3" is bigger than it.

Example 3:

Input: root = [1]
 Output: 1
 Explanation: The root node is a good node.

Idea: dfs, select the maximum value, and count

# 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 goodNodes(self, root: TreeNode) -> int:
        self.ans = 0

        def dfs(node,maxval):
            
            if node:
                if node.val >= maxval:
                    self.ans += 1
                    maxval = node.val
            if node.left:
                dfs(node.left,maxval)
            if node.right:
                dfs(node.right,maxval)
            # dfs(node.left,maxval)
            # dfs(node.right,maxval)
            return self.ans
        
        return dfs(root, -float('inf'))

Problems encountered:

error code:

# 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 goodNodes(self, root: TreeNode) -> int:
        ans = 0

        def dfs(node,maxval):
            
            if node:
                if node.val >= maxval:
                    ans += 1
                    maxval = node.val
            if node.left:
                dfs(node.left,maxval)
            if node.right:
                dfs(node.right,maxval)
            # dfs(node.left,maxval)
            # dfs(node.right,maxval)
            return ans
        
        return dfs(root, -float('inf'))
UnboundLocalError: local variable 'ans' referenced before assignment
    ans += 1
Line 34 in dfs (Solution.py)
    return dfs(root, -float('inf'))
Line 44 in goodNodes (Solution.py)
    ret = Solution().goodNodes(param_1)
Line 63 in _driver (Solution.py)
    _driver()
Line 74 in <module> (Solution.py)

The following solutions were found after searching:

The problem is that ans has not been referenced. The
actual parameter ans outside the function needs to be passed into the function. Self.ans should be added to the actual parameter, and self.ans must also be added to the formal parameter.

Guess you like

Origin blog.csdn.net/weixin_45314061/article/details/130473453