Given a binary tree root, return the number of nodes that are an only child. A node x is an only child if its parent has exactly one child (x).
Constraints
n ≤ 100,000 where n is the number of nodes in root
Finding the Only Child in Binary Tree using BFS Algorithm
We can traverse the binar tree using either Breadth First Search Algorithm or Depth First Search Algorithm. And passing down the number of children of its parent allows us to accumulate the count of only children.
We push the node together with the number of children of node’s parent into the queue. The following C++ code implements the BFS algorithm.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | /** * class Tree { * public: * int val; * Tree *left; * Tree *right; * }; */ int countOnlyChildInBinaryTree(Tree* root) { if (!root) return 0; queue<pair<Tree*, int>> Q; Q.emplace(root, -1); int ans = 0; while (!Q.empty()) { auto [cur, cnt] = Q.front(); Q.pop(); int x = 0; if (cur->left) x ++; if (cur->right) x ++; if (cnt == 1) { ans ++; } if (cur->left) { Q.emplace(cur->left, x); } if (cur->right) { Q.emplace(cur->right, x); } } return ans; } |
/** * class Tree { * public: * int val; * Tree *left; * Tree *right; * }; */ int countOnlyChildInBinaryTree(Tree* root) { if (!root) return 0; queue<pair<Tree*, int>> Q; Q.emplace(root, -1); int ans = 0; while (!Q.empty()) { auto [cur, cnt] = Q.front(); Q.pop(); int x = 0; if (cur->left) x ++; if (cur->right) x ++; if (cnt == 1) { ans ++; } if (cur->left) { Q.emplace(cur->left, x); } if (cur->right) { Q.emplace(cur->right, x); } } return ans; }
Below is the Python code that also implements the same BFS Algorithm.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | # class Tree: # def __init__(self, val, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def countOnlyChildInBinaryTree(self, root): if not root: return 0 q = deque([(root, -1)]) ans = 0 while q: cur, cnt = q.popleft() if cnt == 1: ans += 1 x = 0 if cur.left: x += 1 if cur.right: x += 1 if cur.left: q.append((cur.left, x)) if cur.right: q.append((cur.right, x)) return ans |
# class Tree: # def __init__(self, val, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def countOnlyChildInBinaryTree(self, root): if not root: return 0 q = deque([(root, -1)]) ans = 0 while q: cur, cnt = q.popleft() if cnt == 1: ans += 1 x = 0 if cur.left: x += 1 if cur.right: x += 1 if cur.left: q.append((cur.left, x)) if cur.right: q.append((cur.right, x)) return ans
Both implementations are O(N) time and O(N) space (as we are using a queue) where N is the number of the nodes in the binary tree.
See also: Teaching Kids Programming – Breadth First Search Algorithm to Find the Only Child in Binary Tree
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: Teaching Kids Programming - Two Sum Algorithm when Input Array is Sorted
Next Post: Teaching Kids Programming - Three Sum Algorithm