Density of Binary Tree using Level Order Traversal Last Updated : 19 Oct, 2024 Comments Improve Suggest changes Like Article Like Report Given a Binary Tree, the task is to find the density of it by doing one traversal of it. Density of Binary Tree = Size / Height. Examples: Input: Output: 1.5Explanation: As the height of given tree is 2 and size of given tree is 3 , the density is (3/2) = 1.5Input: Output: 1Explanation: As the height of given tree is 3 and size of given tree is 3 , the density is (3/3) = 1Approach:The idea is to use level order traversal to calculate the height and size of the binary tree in one traversal. While traversing the tree, increment the height after each level of traversal and increment the size after each node. Return the value of (size/ height).Below is the implementation of the above approach: C++ // C++ program to find the density // of given binary tree. #include <bits/stdc++.h> using namespace std; class Node { public: int data; Node* left, *right; Node (int x) { data = x; left = nullptr; right = nullptr; } }; // Given a binary tree, find its density. float findDensity(Node* root) { // base case if (root == nullptr) return 0; int size = 0, height = 0; queue<Node*> q; q.push(root); while (!q.empty()) { int cnt = q.size(); // Traverse each level while (cnt--) { Node* curr = q.front(); q.pop(); size++; if (curr->left != nullptr) q.push(curr->left); if (curr->right != nullptr) q.push(curr->right); } height++; } return (float) size/height; } int main() { // hard coded binary tree. // 10 // / \ // 20 30 Node* root = new Node(10); root->left = new Node(20); root->right = new Node(30); cout << findDensity(root) << endl; return 0; } Java // Java program to find the density // of given binary tree. import java.util.LinkedList; import java.util.Queue; class Node { int data; Node left, right; Node(int x) { data = x; left = null; right = null; } } class GfG { // Given a binary tree, find its density. static float findDensity(Node root) { // base case if (root == null) return 0; int size = 0, height = 0; Queue<Node> q = new LinkedList<>(); q.add(root); while (!q.isEmpty()) { int cnt = q.size(); // Traverse each level while (cnt-- > 0) { Node curr = q.poll(); size++; if (curr.left != null) q.add(curr.left); if (curr.right != null) q.add(curr.right); } height++; } return (float) size / height; } public static void main(String[] args) { // hard coded binary tree. // 10 // / \ // 20 30 Node root = new Node(10); root.left = new Node(20); root.right = new Node(30); System.out.println(findDensity(root)); } } Python # Python program to find the density # of given binary tree. from collections import deque class Node: def __init__(self, x): self.data = x self.left = None self.right = None # Given a binary tree, find # its density. def findDensity(root): # base case if root is None: return 0 size = 0 height = 0 q = deque([root]) while q: cnt = len(q) # Traverse each level while cnt > 0: curr = q.popleft() size += 1 if curr.left is not None: q.append(curr.left) if curr.right is not None: q.append(curr.right) cnt -= 1 height += 1 return size / height if __name__ == "__main__": # hard coded binary tree. # 10 # / \ # 20 30 root = Node(10) root.left = Node(20) root.right = Node(30) print(findDensity(root)) C# // C# program to find the density // of given binary tree. using System; using System.Collections.Generic; class Node { public int data; public Node left, right; public Node(int x) { data = x; left = null; right = null; } } class GfG { // Given a binary tree, find its density. static float findDensity(Node root) { // base case if (root == null) return 0; int size = 0, height = 0; Queue<Node> q = new Queue<Node>(); q.Enqueue(root); while (q.Count > 0) { int cnt = q.Count; // Traverse each level while (cnt-- > 0) { Node curr = q.Dequeue(); size++; if (curr.left != null) q.Enqueue(curr.left); if (curr.right != null) q.Enqueue(curr.right); } height++; } return (float)size / height; } static void Main(string[] args) { // hard coded binary tree. // 10 // / \ // 20 30 Node root = new Node(10); root.left = new Node(20); root.right = new Node(30); Console.WriteLine(findDensity(root)); } } JavaScript // JavaScript program to find the density // of given binary tree. class Node { constructor(x) { this.data = x; this.left = null; this.right = null; } } // Given a binary tree, find its density. function findDensity(root) { // base case if (root === null) return 0; let size = 0, height = 0; let q = []; q.push(root); while (q.length > 0) { let cnt = q.length; // Traverse each level while (cnt-- > 0) { let curr = q.shift(); size++; if (curr.left !== null) q.push(curr.left); if (curr.right !== null) q.push(curr.right); } height++; } return size / height; } // hard coded binary tree. // 10 // / \ // 20 30 let root = new Node(10); root.left = new Node(20); root.right = new Node(30); console.log(findDensity(root)); Output1.5 Time Complexity: O(n) where n is the number of nodes in the binary tree.Auxiliary Space: O(n)Related article:Density of Binary Tree in One Traversal. Comment More infoAdvertise with us Next Article Density of Binary Tree using Level Order Traversal P prajmsidc Follow Improve Article Tags : Tree Queue DSA tree-level-order Practice Tags : QueueTree Similar Reads Density of Binary Tree in One Traversal Given a Binary Tree, the task is to find the density of it by doing one traversal of it. Density of Binary Tree = (Size / Height). Examples: Input: Output: 1.5Explanation: As the height of given tree is 2 and size of given tree is 3 , the density is (3/2) = 1.5Input: Output: 1Explanation: As the hei 6 min read Boundary Level order traversal of a Binary Tree Given a Binary Tree, the task is to print all levels of this tree in Boundary Level order traversal. Boundary Level order traversal: In this traversal, the first element of the level (starting boundary) is printed first, followed by last element (ending boundary). Then the process is repeated for th 11 min read Level order traversal of Binary Tree using Morris Traversal Given a binary tree, the task is to traverse the Binary Tree in level order fashion.Examples: Input: 1 / \ 2 3 Output: 1 2 3 Input: 5 / \ 2 3 \ 6 Output: 5 2 3 6 Approach: The idea is to use Morris Preorder Traversal to traverse the tree in level order traversal.Observations: There are mainly two ob 11 min read Middle To Up-Down Order traversal of a Binary Tree Given a binary tree, the task is to traverse this binary tree from the middle to the up-down order. In Middle to up-down order traversal, the following steps are performed: First, print the middle level of the tree.Then, print the elements at one level above the middle level of the tree.Then, print 15+ min read Specific Level Order Traversal of Binary Tree Given a Binary Tree, the task is to perform a Specific Level Order Traversal of the tree such that at each level print 1st element then the last element, then 2nd element and 2nd last element, until all elements of that level are printed and so on.Examples:Input: Output: 5 3 7 2 8 4 6 9 0 5 1 Explan 8 min read Like