-
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.
Added Question in Love Babbar DSA Sheet
- Loading branch information
1 parent
ffbde4a
commit b989b6c
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
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 @@ | ||
# Coming soon |
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 @@ | ||
# Coming soon |
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 @@ | ||
# Coming soon |
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 @@ | ||
# Coming soon |
104 changes: 104 additions & 0 deletions
104
Love Babbar DSA Sheet/6. Binary Trees/2. Reverse 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,104 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
/*Ques - Given a binary tree of size N, find its reverse level order traversal. ie- the traversal must begin from the last level. | ||
Input - 1 3 2 -1 -1 -1 -1 | ||
Output - 3 2 1 | ||
*/ | ||
|
||
class Node{ | ||
public: | ||
int data; | ||
Node *left; | ||
Node *right; | ||
|
||
Node(int data){ | ||
this->data = data; | ||
left = right = NULL; | ||
} | ||
|
||
~Node(){ | ||
delete left; | ||
delete right; | ||
} | ||
}; | ||
|
||
Node *buildBinaryTree(){ | ||
int rootData; | ||
cout << "Enter root data - "; | ||
cin >> rootData; | ||
|
||
Node *root = new Node(rootData); | ||
|
||
queue<Node *> pendingNodes; | ||
pendingNodes.push(root); | ||
|
||
while (!pendingNodes.empty()){ | ||
Node *front = pendingNodes.front(); | ||
pendingNodes.pop(); | ||
|
||
int leftData; | ||
cout << "Enter Left Child of " << front->data << "\n"; | ||
cin >> leftData; | ||
|
||
if (leftData != -1){ | ||
Node *leftChild = new Node(leftData); | ||
front->left = leftChild; | ||
pendingNodes.push(leftChild); | ||
} | ||
|
||
int rightData; | ||
cout << "Enter Right Child of " << front->data << "\n"; | ||
cin >> rightData; | ||
|
||
if (rightData != -1){ | ||
Node *rightChild = new Node(rightData); | ||
front->right = rightChild; | ||
pendingNodes.push(rightChild); | ||
} | ||
} | ||
|
||
return root; | ||
} | ||
|
||
vector<int> reverseLevelOrder(Node *root){ | ||
vector<int> ans; | ||
stack<Node *> s; | ||
queue<Node *> q; | ||
q.push(root); | ||
|
||
while (!q.empty()){ | ||
Node *front = q.front(); | ||
q.pop(); | ||
s.push(front); | ||
|
||
if (front->right) | ||
q.push(front->right); | ||
|
||
if (front->left) | ||
q.push(front->left); | ||
} | ||
|
||
while (!s.empty()){ | ||
Node *temp = s.top(); | ||
ans.push_back(temp->data); | ||
s.pop(); | ||
} | ||
|
||
return ans; | ||
} | ||
|
||
int main(){ | ||
|
||
Node *root = buildBinaryTree(); | ||
|
||
vector<int> ans = reverseLevelOrder(root); | ||
|
||
for(int e : ans) | ||
cout << e << " "; | ||
|
||
delete root; | ||
return 0; | ||
} |