Loading...
Loading...
You are given the root of a binary tree. The node values carry no meaning here, and every node holds 0.
You can put a monitor on any node. A monitor covers:
Return the smallest number of monitors that leaves every node of the tree covered.
Example 2: two monitors cover the whole tree.
Your function receives root as a TreeNode. The node type is provided for you, with val, left, and right fields.
The examples below write the tree as its level-order traversal, where null marks a missing child of a listed node and trailing nulls are omitted. That is only how the input is displayed; the decoding is done for you.
Node.val =0Put the monitor on the left child of the root. It covers itself, its parent the root, and both of its own children, which is all four nodes.
This tree is a path of four nodes with one extra node hanging off the bottom. A monitor at depth 1 covers the top three nodes, and a second monitor at depth 3 covers the last two. No single monitor reaches all five.