Print the nodes having exactly one child in a Binary tree using JavaScript
Last Updated :
14 Jun, 2024
Given a binary tree, our task is to return the number of nodes in the Binary tree that have at least one child otherwise return “-1” if no such node exists.
Examples:
Input:
1
/ \
2 3
/ \
4 5
/
6
Output: 3
Explanation:
There are three nodes in the Binary tree that have at least one child that are 1,2,4.
Input:
9
/ \
7 8
/ \
4 3
Output: 2
Explanation: There are two nodes in the Binary tree that have
at least one child that are 9,.8
Below are the approaches to return the number of nodes in the Binary tree that have at least one child using JavaScript:
In this approach, we use recursive depth-first search (DFS) to traverse the binary tree. We start from the root node and recursively visit each node in a depth-first manner. At each node, we check if it has at least one child. If it does, we increment the count. Finally, we return the total count.
Example: The example below shows the program to return the number of nodes in the Binary tree that have at least one child using Recursive Depth-First Search (DFS).
JavaScript
// Definition for a binary tree node.
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
// Function to count nodes with at least
// one child in a binary tree
function countNodesWithChildren(root) {
// Base case: if the node is null, return 0
if (!root) {
return 0;
}
// Recursive DFS traversal
// Count nodes with at least one child
// in the left and right subtrees
let count = countNodesWithChildren(root.left)
+ countNodesWithChildren(root.right);
// If the current node has at least one child,
// increment count
if (root.left || root.right) {
count++;
}
// Return the count
return count;
}
// Example usage:
// Constructing a binary tree
let root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.left = new TreeNode(6);
// Counting nodes with at least one child
let result = countNodesWithChildren(root);
console.log("Number of nodes with at least one child:", result);
OutputNumber of nodes with at least one child: 3
Time Complexity: O(N), where N is the number of nodes in the binary tree.
Auxiliary Space: O(H), where H is the height of the binary tree.
Using Iterative Breadth-First Search (BFS) using Queue
In this approach, we use iterative breadth-first search (BFS) with a queue data structure to traverse the binary tree. We start by enqueuing the root node and iteratively dequeue nodes from the queue. At each node, we check if it has at least one child. If it does, we increment the count and enqueue its children if they exist. We continue this process until the queue is empty.
Example: Implementation of a program to return the number of nodes in the Binary tree that have at least one child using Iterative Breadth-First Search (BFS) using Queue.
JavaScript
// Definition for a binary tree node.
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
// Function to count nodes with at least
// one child in a binary tree
function countNodesWithChildren(root) {
// Base case: if the root is null, return 0
if (!root) {
return 0;
}
// Initialize a queue for BFS traversal
let queue = [root];
let count = 0;
// Perform BFS traversal
while (queue.length > 0) {
// Dequeue the front node
let node = queue.shift();
// If the dequeued node has at least one child,
// increment count
if (node.left || node.right) {
count++;
}
// Enqueue left and right children if they exist
if (node.left) {
queue.push(node.left);
}
if (node.right) {
queue.push(node.right);
}
}
// Return the count
return count;
}
// Example usage:
// Constructing a binary tree
let root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.left = new TreeNode(6);
// Counting nodes with at least one child
let result = countNodesWithChildren(root);
console.log("Number of nodes with at least one child:", result);
OutputNumber of nodes with at least one child: 3
Time Complexity: O(N), where N is the number of nodes in the binary tree.
Auxiliary Space: O(N), where N is the number of nodes in the binary tree.
Similar Reads
Count the Number of Nodes in a Complete Binary tree using JavaScript
We need to count the number of the nodes in the complete binary tree using JavaScript. The complete binary tree is a binary tree in which every level then except possibly the last, is completely filled and all the nodes are as far left as possible. There are several approaches for counting the numbe
4 min read
Find the Preorder Successor of a Given Node in a Binary Tree using JavaScript
The Preorder successor of a given node is a node that occurs after the given node in the preorder traversal of the binary tree. The preorder traversal is a traversal in which the root node is visited first, then the left child, and then the right child. Example: The pre-order successor of a binary t
2 min read
Find the Distance Between Two Nodes in the Binary Tree using JavaScript
Given two nodes, our task is to find the distance between two nodes in a binary tree using JavaScript, no parent pointers are given. The distance between two nodes is the minimum number of edges to be traversed to reach one node from another. Examples: Input: Binary Tree as described above in diagra
5 min read
Find the Sum of All Left Leaves in a Given Binary Tree using JavaScript
Given a Binary Tree, find the sum of all left leaves in it. A binary tree is a hierarchical data structure in which each node has at most two children, referred to as the left child and the right child. Finding the sum of all left leaves involves traversing the entire binary tree and checking each n
4 min read
Convert a Binary Tree to a Binary Search Tree using JavaScript
Given a Binary Tree, the task is to convert it to a Binary Search Tree. The conversion must be done in such a way that it keeps the original structure of the Binary Tree. Example: To demonstrate the conversion of the Binary Tree to the Binary Search Tree. Input: 10 / \ 2 7 / \ 8 4Output: 8 / \ 4 10
2 min read
Check Symmetrical Binary Tree using JavaScript
Given a binary tree, our task is to check whether it is Symmetrical Binary tree. In other words, we need to check whether the binary tree is a mirror of itself. Example: Input: 11 / \ 12 12 / \ / \ 13 14 14 13Output: True Input: 11 / \ 12 12 \ \ 13 13Output: FalseBelow are the approaches to check Sy
3 min read
How to Check if an element is a child of a parent using JavaScript?
In this article, we are going to see the methods by which we can Check if an element is a child of a parent using JavaScript. These are the following methods: Table of Content Using the Node.contains() methodLooping through the parents of the given childUsing the hasChildNodes() methodMethod 1: Usin
4 min read
JavaScript Program to Print all Nodes that do not have any Siblings
Given a Binary Tree, our task is to print all nodes that do not have a sibling (a sibling is a node with the same parent. In a Binary Tree, there can be at most one sibling). The root should not be printed as it cannot have a sibling. Example: Input: Output: 4 5 6Explanation: As the node 4,5, and 6
3 min read
Boundary Traversal of Binary Tree using JavaScript
The Boundary Traversal of the Binary Tree can be done by traversing the left, right, and leaf parts of the Binary Tree. We will break the Boundary Traversal of Binary Tree using JavaScript in three parts. First, we will traverse the left part, right part, and leaf parts of the Binary Tree and print
4 min read
Find the maximum path sum between two leaves of a binary tree using JavaScript
Given the root of a binary tree, return the maximum path sum of any non-empty path. A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to p
6 min read