-
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.
New Questions added in Love babbar dsa sheet
- Loading branch information
1 parent
b989b6c
commit e4dc69f
Showing
1 changed file
with
112 additions
and
0 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
Love Babbar DSA Sheet/6. Binary Trees/3. Height of a tree/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,112 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
/*Ques - Given a binary tree, find its height. | ||
Input - 1 3 2 -1 -1 -1 -1 | ||
Output - 2 | ||
*/ | ||
|
||
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; | ||
} | ||
|
||
//Recursive solution | ||
int heightOfBinaryTreeUsingRecursion(Node* root){ | ||
if(root == NULL) | ||
return 0; | ||
|
||
int lHeight = heightOfBinaryTreeUsingRecursion(root -> left); | ||
int rHeight = heightOfBinaryTreeUsingRecursion(root -> right); | ||
|
||
return (max(lHeight, rHeight) + 1); | ||
} | ||
|
||
//Iterative solution | ||
int heightOfBinaryTreeUsingQueue(Node* root){ | ||
queue<Node*> q; | ||
q.push(root); | ||
|
||
int leftLevel = 0; | ||
int rightLevel = 0; | ||
|
||
while(!q.empty()){ | ||
Node* front = q.front(); | ||
q.pop(); | ||
|
||
if(front -> left){ | ||
leftLevel++; | ||
q.push(front -> left); | ||
} | ||
|
||
if(front -> right){ | ||
rightLevel++; | ||
q.push(front -> right); | ||
} | ||
} | ||
return (max(leftLevel, rightLevel) + 1); | ||
} | ||
|
||
int main(){ | ||
|
||
Node *root = buildBinaryTree(); | ||
|
||
cout << "Height of given binary tree using recursion is - " << heightOfBinaryTreeUsingRecursion(root) << "\n"; | ||
|
||
cout << "Height of given binary tree using queue is - " << heightOfBinaryTreeUsingQueue(root); | ||
|
||
delete root; | ||
return 0; | ||
} |