📅  最后修改于: 2023-12-03 14:58:22.376000             🧑  作者: Mango
This is an introduction to the programming problem titled "GATE CS Mock 2018 - Set 2 - Question 32". The problem is a part of the GATE (Graduate Aptitude Test in Engineering) Computer Science (CS) Mock examination conducted in 2018. The problem aims to test the programming and problem-solving skills of the candidates.
The problem statement goes as follows:
Given a binary tree
T
, the task is to count the number of nodes inT
which have all immediate children as leaf nodes.
The problem deals with a binary tree data structure and requires counting the number of nodes that have all their immediate children as leaf nodes. The input is a binary tree and the output should be the count of such nodes.
Let's understand the problem with an example:
Input:
10
/ \
7 8
/ \
5 6
/ \ / \
3 4 2 1
Output: 2
Explanation: In the given binary tree, the nodes 7 and 8 are the only two nodes that have all their immediate children as leaf nodes.
An efficient approach to solving this problem can be achieved using a recursive depth-first search (DFS) on the binary tree. We can recursively check for each node in the tree if all its immediate children are leaf nodes. If so, we increment the count.
Pseudocode for the recursive approach:
def countNodesWithAllLeafChildren(node):
if node is None:
return 0
if node.left is None and node.right is None:
return 1
left_count = countNodesWithAllLeafChildren(node.left)
right_count = countNodesWithAllLeafChildren(node.right)
# Check if both children are leaf nodes
if node.left and node.right and node.left.left is None and node.left.right is None and node.right.left is None and node.right.right is None:
return left_count + right_count + 1
return left_count + right_count
The time complexity for this approach is O(n), where n is the number of nodes in the input binary tree. This is because we are traversing each node in the tree exactly once.
The space complexity is O(h), where h is the height of the binary tree. This is due to the recursion stack used during the recursive DFS traversal.
Here's an example implementation in Python:
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def countNodesWithAllLeafChildren(node):
if node is None:
return 0
if node.left is None and node.right is None:
return 1
left_count = countNodesWithAllLeafChildren(node.left)
right_count = countNodesWithAllLeafChildren(node.right)
if node.left and node.right and node.left.left is None and node.left.right is None and node.right.left is None and node.right.right is None:
return left_count + right_count + 1
return left_count + right_count
# Example usage
root = Node(10)
root.left = Node(7)
root.right = Node(8)
root.left.left = Node(5)
root.left.right = Node(6)
root.right.left = Node(2)
root.right.right = Node(1)
root.left.left.left = Node(3)
root.left.left.right = Node(4)
result = countNodesWithAllLeafChildren(root)
print("Number of nodes with all leaf children:", result)
The output of this program will be:
Number of nodes with all leaf children: 2
By using the provided approach and implementation, the program correctly counts the number of nodes in the binary tree that have all their immediate children as leaf nodes.