Level Order Traversal LIne by Line code in c++.

Check for Children-sum property in binary tree

In this article, we will learn the approach and code about how to check for the children-sum property in binary tree in C++. Children sum property is true if the sum of the value of left node and right node is equal to the parent node value.

Level order traversal line by line in C++
level order traversal line by line in C++

Steps for Level Order Traversal

  • Step 1 : Push the root i.e. 50 to the queue.
  • Step 2 : Pop the element 50 from the queue and print it.
  • Step 3 : Now, Add it’s left and right child i.e. add 30 and 70 to queue.
  • Step 4 : Again pop the front element i.e. 30 from queue and print it .
  • Step 5 :  Add it’s left and right child i.e. 10 and 40 in the queue.
  • Step 6 : Pop the element 70 from the queue and  print it.
  • Step 7 : add it’s left and right child i.e. 60 and 90.
  • Step 8 : Now pop all the elements from the queue and print them as there is no child of these elements.
      Therefore the printing sequence will be 50 30 70 10 40 60 90 .

Algorithm

  1. If the root is NULL, return.
  2. Otherwise push the root in queue.
  3. Pop the node from the queue.
  4. Print the node’s data and add its left and right child.
  5. Repeat until the queue is empty.

Code Implementation for level order traversal in binary tree in C++

Run
#include<bits/stdc++.h>
using namespace std;

struct TreeNode
{
  int val;
  TreeNode *left;
  TreeNode *right;
    TreeNode (int x):val (x), left (NULL), right (NULL)
  {
  }
};

void levelOrder (TreeNode * root)
{
  if (!root)
    return;

  queue < TreeNode * >q;
  q.push (root);

  while (!q.empty ())
    {
      int size = q.size ();
      for (int i = 0; i < size; i++)
	{
	  TreeNode *node = q.front ();
	  q.pop ();
	  cout << node->val << " ";

	  if (node->left)
	    q.push (node->left);
	  if (node->right)
	    q.push (node->right);
	}
      cout << endl;
    }
}

int main ()
{
  TreeNode *root = new TreeNode (3);
  root->left = new TreeNode (9);
  root->right = new TreeNode (20);
  root->right->left = new TreeNode (15);
  root->right->right = new TreeNode (7);
  levelOrder (root);
  return 0;
}

Output:
3
9 20
15 7

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java