Home > Blockchain >  Problem with understanding the code of 968. Binary Tree Cameras
Problem with understanding the code of 968. Binary Tree Cameras

Time:10-31

I am studying algorithms and trying to solve the LeetCode problem 968. Binary Tree Cameras:

You are given the root of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children.

Return the minimum number of cameras needed to monitor all nodes of the tree.

I got stuck on it, and after checking the discussion I better understood the logic, but I am still struggling to understand the code:

def minCameraCover(self, root):
    self.res = 0
    def dfs(root):
        if not root: return 2
        l, r = dfs(root.left), dfs(root.right)
        if l == 0 or r == 0:
            self.res  = 1
            return 1
        return 2 if l == 1 or r == 1 else 0
    return (dfs(root) == 0)   self.res

I don't understand why l, r == 0, 0 in a DFS function while the base case is set as if not root: return 2

What are the mechanics behind this that makes dfs(root.left), def(root.right) return 0?

So far I understood that a node has three states:

  • 0: it's a leaf
  • 1: it has a camera and the node is parent of a leaf
  • 2: it's being covered, but does not have a camera on it.

CodePudding user response:

The base case is set for a None, i.e. the absence of a node. Such a virtual position is never a problem, so we can count it as "covered", but there is no camera there. This is why the base case returns 2.

Now when a leaf node is encountered, then obviously both recursive calls will get None as argument and return 2.

Then the expression 2 if l == 1 or r == 1 else 0 will evaluate to 0, as neither l nor r are 1 (they are 2): theelse clause kicks in.

I hope this clarifies that for leaf nodes the return value will be 0, but also for other nodes this can happen: every time both recursive calls return 2, the caller will itself return 0.

Therefore the better explanation of the three states is:

  • 1: the node has a camera
  • 2: the node has no camera, but it is covered from below
  • 0: the node has no camera yet and is not covered from below. If it has a parent, that parent should get a camera so to cover for this node. It it is the root, it must get a camera itself.
  • Related