-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated Binary Tree Section in Love Babbar DSA sheet
- Loading branch information
1 parent
8686fcc
commit f2d6c7e
Showing
2 changed files
with
97 additions
and
16 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
Love Babbar DSA Sheet/6. Binary Trees/1. Level order traversal/code.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
/*Ques - Given a binary tree, find its level order traversal. | ||
Input - 1 3 2 -1 -1 -1 -1 | ||
Output - 1 3 2 | ||
*/ | ||
|
||
template <typename T> class Node{ | ||
public: | ||
T data; | ||
Node* left; | ||
Node* right; | ||
|
||
Node(T data){ | ||
this -> data = data; | ||
left = right = NULL; | ||
} | ||
|
||
~Node(){ | ||
delete left; | ||
delete right; | ||
} | ||
}; | ||
|
||
Node<int>* buildBinaryTree(){ | ||
int rootData; | ||
cout << "Enter root data - "; | ||
cin >> rootData; | ||
|
||
Node<int>* root = new Node<int>(rootData); | ||
|
||
queue<Node<int>*> pendingNodes; | ||
pendingNodes.push(root); | ||
|
||
while(!pendingNodes.empty()){ | ||
Node<int>* front = pendingNodes.front(); | ||
pendingNodes.pop(); | ||
|
||
int leftData; | ||
cout << "Enter Left Child of " << front -> data << "\n"; | ||
cin >> leftData; | ||
|
||
if(leftData != -1){ | ||
Node<int>* leftChild = new Node<int>(leftData); | ||
front -> left = leftChild; | ||
pendingNodes.push(leftChild); | ||
} | ||
|
||
int rightData; | ||
cout << "Enter Right Child of " << front -> data << "\n"; | ||
cin >> rightData; | ||
|
||
if(rightData != -1){ | ||
Node<int>* rightChild = new Node<int>(rightData); | ||
front -> right = rightChild; | ||
pendingNodes.push(rightChild); | ||
} | ||
} | ||
|
||
return root; | ||
} | ||
|
||
void solution(Node<int>* root){ | ||
queue<Node<int>*> pendingNodes; | ||
pendingNodes.push(root); | ||
|
||
cout << root -> data << " "; | ||
|
||
while(!pendingNodes.empty()){ | ||
Node<int>* front = pendingNodes.front(); | ||
pendingNodes.pop(); | ||
|
||
if(front -> left){ | ||
cout << front -> left -> data << " "; | ||
pendingNodes.push(front -> left); | ||
} | ||
|
||
if(front -> right){ | ||
cout << front -> right -> data << " "; | ||
pendingNodes.push(front -> right); | ||
} | ||
} | ||
cout << "\n"; | ||
} | ||
|
||
int main(){ | ||
|
||
Node<int>* root = buildBinaryTree(); | ||
|
||
solution(root); | ||
|
||
delete root; | ||
return 0; | ||
} |
This file was deleted.
Oops, something went wrong.