870. Count Half Nodes in Binary Tree

EasyTreesBinary TreeDFSRecursion

Given the root of a binary tree, return the number of half nodes — nodes that have exactly one child (either left or right, but not both). The tree is given as a level-order array with null for missing children.

Input: A level-order array of the tree.

Output: Integer — the number of half nodes.

Examples

Example 1
Input: [1,2,3,4,null,null,7]
Output: 2
Explanation: Nodes 2 and 3 each have exactly one child.
Example 2
Input: [1,2,3]
Output: 0
Explanation: No half nodes.
Example 3
Input: [1,2]
Output: 1
Explanation: Root has one child.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →