Trees
Trees
Input:
1
/ \
3 2
Output: 1 3
Example 2:
Input:
Output: 10 20 40
Your Task:
You just have to complete the function leftView() that prints the left view. The newline is automatically
appended by the driver code.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Constraints:
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000
Code:
#include <bits/stdc++.h>
using namespace std;
// Tree Node
struct Node
{
int data;
Node* left;
Node* right;
};
istringstream iss(str);
for(string str; iss >> str; )
ip.push_back(str);
// for(string i:ip)
// cout<<i<<" ";
// cout<<endl;
// Create the root of the tree
Node* root = newNode(stoi(ip[0]));
return root;
}
int main() {
int t;
scanf("%d ",&t);
while(t--)
{
string s;
getline(cin,s);
Node* root = buildTree(s);
leftView(root);
cout << endl;
}
return 0;
}
struct Node
{
int data;
struct Node* left;
struct Node* right;
Node(int x){
data = x;
left = right = NULL;
}
};
*/
int flag=0;
vector<int> v;
int height(Node* root)
{
if(root==NULL)
return 0;
int hl=height(root->left);
int hr=height(root->right);
return max(hl,hr)+1;
}
for(int i=1;i<=h;i++)
{
flag=0;
perlevel(root,i);
}
return;
}
// A wrapper over leftViewUtil()
void leftView(Node *root)
{
level(root);
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
v.clear();
}
Input:
2
/ \
1 3
Output: 1
Example 2:
Input:
2
\
7
\
6
\
5
\
9
\
2
\
6
Output: 0
Your Task:
You don't need to read input or print anything. Your task is to complete the function isBST() which takes the
root of the tree as a parameter and returns true if the given binary tree is BST, else returns false. The printing
is done by the driver's code.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the BST).
Constraints:
0 <= Number of edges <= 100000
Code:
#include <bits/stdc++.h>
using namespace std;
// Tree Node
struct Node {
int data;
Node *left;
Node *right;
Node(int val) {
data = val;
};
// Corner Case
vector<string> ip;
istringstream iss(str);
ip.push_back(str);
queue<Node*> queue;
queue.push(root);
int i = 1;
queue.pop();
// Get the current node's value from the string
if(currVal != "N") {
queue.push(currNode->left);
i++;
break;
currVal = ip[i];
if(currVal != "N") {
queue.push(currNode->right);
i++;
return root;
if(root==NULL)
return;
inorder(root->left, v);
v.push_back(root->data);
inorder(root->right, v);
int main() {
int t;
string tc;
getline(cin, tc);
t=stoi(tc);
while(t--)
string s;
getline(cin, s);
return 0;
struct Node {
int data;
Node *left;
Node *right;
Node(int val) {
data = val;
};
*/
if(node==NULL)
return 1;
if(node->data<min||node->data>max)
return 0;
else
checkNode(node->left,min,node->data-1)&&
checkNode(node->right,node->data+1,max);
return checkNode(root,INT_MIN,INT_MAX);
}
// { Driver Code Starts
Example 1:
Input:
1
/ \
3 2
Output: 3 1 2
Explanation:
First case represents a tree with 3 nodes
and 2 edges where root is 1, left child of
1 is 3 and right child of 1 is 2.
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output: 40 20 60 30
Your Task:
This is a functional problem, you don't need to care about input, just complete the function bottomView()
which takes the root node of the tree as input and returns an array containing the bottom view of the given
tree.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= Number of nodes <= 105
1 <= Data of a node <= 105
Code:
#include <bits/stdc++.h>
// Tree Node
struct Node
int data;
Node* left;
Node* right;
};
temp->data = val;
temp->left = NULL;
temp->right = NULL;
return temp;
// Corner Case
return NULL;
vector<string> ip;
istringstream iss(str);
ip.push_back(str);
queue<Node*> queue;
queue.push(root);
int i = 1;
while(!queue.empty() && i < ip.size()) {
queue.pop();
if(currVal != "N") {
currNode->left = newNode(stoi(currVal));
queue.push(currNode->left);
i++;
break;
currVal = ip[i];
// If the right child is not null
if(currVal != "N") {
currNode->right = newNode(stoi(currVal));
queue.push(currNode->right);
i++;
return root;
int main() {
int t;
string tc;
getline(cin, tc);
t=stoi(tc);
while(t--)
string s ,ch;
getline(cin, s);
return 0;
struct Node
Node(int key)
data = key;
left = right = NULL;
}; */
vector<int> ans;
if(root==NULL)
return ans;
queue<pair<Node*,int>> q;
map<int,int> mp;
int d=0;
q.push(make_pair(root,0));
mp[d]=root->data;
while(!q.empty())
pair<Node*,int> temp=q.front();
q.pop();
d=temp.second;
mp[d]=temp.first->data;
if(temp.first->left!=NULL)
q.push(make_pair(temp.first->left,d-1));
if(temp.first->right!=NULL)
q.push(make_pair(temp.first->right,d+1));
while(it!=mp.end())
//cout<<it->first<<" - "<<it->second<<endl;
if(it->second!=0)
ans.push_back(it->second);
it++;
return ans;
}
4. Vertical Traversal of Binary Tree
Given a Binary Tree, find the vertical traversal of it starting from the leftmost level to the rightmost level.
If there are multiple nodes passing through a vertical line, then they should be printed as they appear in level
order traversal of the tree.
Example 1:
Input:
2
\
3
/
4
Output: 2 4 3
Example 2:
Input:
1
/ \
2 3
/ \ \
4 5 6
Output: 4 2 1 5 3 6
Your Task:
You don't need to read input or print anything. Your task is to complete the function verticalOrder() which
takes the root node as input and returns an array containing the vertical order traversal of the tree from the
leftmost to the rightmost level. If 2 nodes lie in the same vertical level, they should be printed in the order they
appear in the level order traversal of the tree.
Expected Time Complexity: O(N log N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= Number of nodes <= 5000
Code:
// Tree Node
struct Node
int data;
Node* left;
Node* right;
};
temp->data = val;
temp->left = NULL;
temp->right = NULL;
return temp;
// Corner Case
if(str.length() == 0 || str[0] == 'N')
return NULL;
vector<string> ip;
istringstream iss(str);
ip.push_back(str);
queue<Node*> queue;
queue.push(root);
int i = 1;
queue.pop();
// Get the current node's value from the string
if(currVal != "N") {
currNode->left = newNode(stoi(currVal));
queue.push(currNode->left);
i++;
break;
currVal = ip[i];
if(currVal != "N") {
currNode->right = newNode(stoi(currVal));
// Push it to the queue
queue.push(currNode->right);
i++;
return root;
if(!root)
return;
printInorder(root->left);
cout<<root->data<<" ";
printInorder(root->right);
int main() {
int t;
string tc;
getline(cin,tc);
t=stoi(tc);
while(t--)
string s;
getline(cin,s);
// string c;
// getline(cin,c);
return 0;
struct Node
{
int data;
Node(int x){
data = x;
};
*/
if(p1.second<p2.second)
return true;
return false;
vector<int> res;
if(root==NULL)
return res;
multimap<int,int> m;
queue< pair<Node*,int> >q;
q.push(make_pair(root,0));
while(!q.empty())
pair<Node*,int> curr=q.front();
q.pop();
m.insert(pair<int,int>(curr.second,curr.first->data));
if(curr.first->left)
q.push(make_pair(curr.first->left,curr.second-1));
if(curr.first->right)
q.push(make_pair(curr.first->right,curr.second+1));
for(auto i:m)
res.push_back(i.second);
return res;
Input:
1
/ \
3 2
Output:1 3 2
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output: 10 20 30 60 40
Your Task:
The task is to complete the function printSpiral() which prints the elements in spiral form of level order
traversal. The newline is automatically appended by the driver code.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
0 <= Number of nodes <= 105
1 <= Data of a node <= 105
Code:
#include <bits/stdc++.h>
// Tree Node
struct Node
int data;
Node* left;
Node* right;
};
temp->data = val;
temp->left = NULL;
temp->right = NULL;
return temp;
// Corner Case
return NULL;
vector<string> ip;
istringstream iss(str);
ip.push_back(str);
queue.push(root);
int i = 1;
queue.pop();
if(currVal != "N") {
currNode->left = newNode(stoi(currVal));
queue.push(currNode->left);
break;
currVal = ip[i];
if(currVal != "N") {
currNode->right = newNode(stoi(currVal));
queue.push(currNode->right);
i++;
return root;
int main() {
int t;
string tc;
getline(cin,tc);
t=stoi(tc);
while(t--)
string s;
getline(cin,s);
printSpiral(root);
return 0;
struct Node
int data;
data = x;
}; */
int flag;
if(root==NULL)
return 0;
int hl=height(root->left);
int hr=height(root->right);
return max(hr,hl)+1;
if(root==NULL)
return;
if(h==1)
cout<<root->data<<" ";
return;
if(!flag)
perlevel(root->left,h-1,flag);
perlevel(root->right,h-1,flag);
// flag=!flag;
if(flag)
perlevel(root->right,h-1,flag);
perlevel(root->left,h-1,flag);
// flag=!flag;
return;
{
if(root==NULL)
return;
int h=height(root);
flag=1;
for(int i=1;i<=h;i++)
perlevel(root,i,flag);
flag=!flag;
return;
Example 1:
Input:
3
/ \
1 2
Output:
3 1 2
1 3 2
Explanation:The connected tree is
3 ------> NULL
/ \
1-----> 2 ------ NULL
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
10 20 30 40 60
40 20 60 10 30
Explanation:The connected tree is
10 ----------> NULL
/ \
20 ------> 30 -------> NULL
/ \
40 ----> 60 ----------> NULL
Your Task:
You don't have to take input. Complete the function connect() that takes root as parameter and connects the
nodes at same level. The printing is done by the driver code.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000
Code:
#include <bits/stdc++.h>
// Tree Node
struct Node
int data;
Node* left;
Node* right;
Node* nextRight;
};
temp->data = val;
temp->left = NULL;
temp->right = NULL;
temp->nextRight = NULL;
return temp;
}
// Function to Build Tree
// Corner Case
return NULL;
vector<string> ip;
istringstream iss(str);
ip.push_back(str);
queue<Node*> queue;
queue.push(root);
int i = 1;
queue.pop();
if(currVal != "N") {
currNode->left = newNode(stoi(currVal));
queue.push(currNode->left);
i++;
break;
currVal = ip[i];
currNode->right = newNode(stoi(currVal));
queue.push(currNode->right);
i++;
return root;
if (root == NULL)
return;
Node* next_root=NULL;
next_root = root->left;
next_root = root->right;
root = root->nextRight;
printSpecial(next_root);
if (root == NULL)
return;
inorder(root->left);
inorder(root->right);
}
/* Driver program to test size function*/
int main()
int t;
scanf("%d\n", &t);
while (t--)
string s;
getline(cin, s);
connect(root);
printSpecial(root);
cout<<endl;
inorder(root);
cout<<endl;
return 0;
/* struct Node
{
int data;
}; */
queue<Node*> q;
q.push(root);
while(!q.empty())
int size=q.size();
while(size--)
Node* p=q.front();
q.pop();
if(size)
p->nextRight=q.front();
if(p->left!=NULL)
q.push(p->left);
if(p->right!=NULL)
q.push(p->right);
Input:
5
/ \
4 6
/ \
3 7
\
8
n1 = 7, n2 = 8
Output: 7
Example 2:
Input:
2
/ \
1 3
n1 = 1, n2 = 3
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function LCA() which takes the root
Node of the BST and two integer values n1 and n2 as inputs and returns the Lowest Common Ancestor of the
Nodes with values n1 and n2 in the given BST.
Expected Time Complexity: O(Height of the BST).
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1 <= N <= 104
Code:
#include <bits/stdc++.h>
struct Node {
int data;
Node *left;
Node *right;
Node(int val) {
data = val;
};
// Corner Case
if(str.length() == 0 || str[0] == 'N')
return NULL;
vector<string> ip;
istringstream iss(str);
ip.push_back(str);
queue<Node*> queue;
queue.push(root);
int i = 1;
queue.pop();
// Get the current node's value from the string
if(currVal != "N") {
queue.push(currNode->left);
i++;
break;
currVal = ip[i];
if(currVal != "N") {
queue.push(currNode->right);
i++;
return root;
int main()
int t;
scanf("%d ",&t);
while(t--)
string s;
int l , h;
getline(cin,s);
scanf("%d ",&l);
scanf("%d ",&h);
return 1;
struct Node {
int data;
Node *left;
Node *right;
Node(int val) {
data = val;
};
*/
while(root!=NULL)
{
root=root->left;
root=root->right;
else
break;
return root;
Given a Binary Tree (BT), convert it to a Doubly Linked List(DLL) In-Place. The left and right pointers in nodes
are to be used as previous and next pointers respectively in converted DLL. The order of nodes in DLL must
be same as Inorder of the given Binary Tree. The first node of Inorder traversal (leftmost node in BT) must be
the head node of the DLL.
Example 1:
Input:
1
/ \
3 2
Output:
3 1 2
2 1 3
Explanation: DLL would be 3<=>1<=>2
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
40 20 60 10 30
30 10 60 20 40
Explanation: DLL would be
40<=>20<=>60<=>10<=>30.
Your Task:
You don't have to take input. Complete the function bToDLL() that takes root node of the tree as a parameter
and returns the head of DLL . The driver code prints the DLL both ways.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(H).
Note: H is the height of the tree and this space is used implicitly for recursion stack.
Constraints:
1 <= Number of nodes <= 1000
1 <= Data of a node <= 1000
Code:
#include <bits/stdc++.h>
// Tree Node
struct Node
int data;
Node* left;
Node* right;
};
// Utility function to create a new Tree Node
temp->data = val;
temp->left = NULL;
temp->right = NULL;
return temp;
// Corner Case
return NULL;
vector<string> ip;
istringstream iss(str);
queue<Node*> queue;
queue.push(root);
int i = 1;
queue.pop();
if(currVal != "N") {
currNode->left = newNode(stoi(currVal));
// Push it to the queue
queue.push(currNode->left);
i++;
break;
currVal = ip[i];
if(currVal != "N") {
currNode->right = newNode(stoi(currVal));
queue.push(currNode->right);
i++;
return root;
}
Node* bToDLL(Node *root);
while (node!=NULL)
prev = node;
node = node->right;
while (prev!=NULL)
prev = prev->left;
if (root != NULL)
inorder(root->left);
inorder(root->right);
int main()
int t;
cin >> t;
getchar();
while (t--)
string inp;
getline(cin, inp);
printList(head);
}
return 0;
struct Node
int data;
Node(int x){
data = x;
};
*/
Node *h;
if(root==NULL)
return ;
fun(root->left);
if(p==NULL)
h=root;
else
p->right=root;
root->left=p;
p=root;
fun(root->right);
}
h=NULL;
p=NULL;
fun(root);
return h;
Input:
1 1
/ \ / \
2 3 2 3
Output: Yes
Explanation: There are two trees both
having 3 nodes and 2 edges, both trees
are identical having the root as 1,
left child of 1 is 2 and right child
of 1 is 3.
Example 2:
Input:
1 1
/ \ / \
2 3 3 2
Output: No
Explanation: There are two trees both
having 3 nodes and 2 edges, but both
trees are not identical.
Your task:
Since this is a functional problem you don't have to worry about input, you just have to complete the
function isIdentical() that takes two roots as parameters and returns true or false. The printing is done by the
driver code.
Code:
#include <bits/stdc++.h>
struct Node
{
int data;
Node(int x){
data = x;
left = NULL;
right = NULL;
};
// Corner Case
return NULL;
vector<string> ip;
istringstream iss(str);
for (string str; iss >> str;)
ip.push_back(str);
queue.push(root);
int i = 1;
queue.pop();
if (currVal != "N") {
queue.push(currNode->left);
i++;
if (i >= ip.size())
break;
currVal = ip[i];
if (currVal != "N") {
queue.push(currNode->right);
i++;
return root;
}
int main() {
int tc;
while (tc--) {
getline(cin, str);
getline(cin, str1);
if (isIdentical(rootA, rootB)) {
} else {
return 0;
struct Node
{
int data;
Node(int x){
data = x;
};
*/
r2 are identical */
if(r1==NULL&&r2==NULL)
return true;
if(r1==NULL&&r2!=NULL)
return false;
}
if(r1!=NULL&&r2==NULL)
return false;
if (r1->data!=r2->data)
return false;
return isIdentical(r1->right,r2->right)&&isIdentical(r1->left,r2->left);
Input:
5
/ \
1 1
/ \
2 2
Output: True
Explanation: Tree is mirror image of
itself i.e. tree is symmetric
Example 2:
Input:
5
/ \
10 10
/ \ \
20 20 30
Output: False
Your Task:
You don't need to read input or print anything. Your task is to complete the function isMirror() which takes the
root of the Binary Tree as its input and returns True if the given Binary Tree is a same as the Mirror image of
itself. Else, it returns False.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Constraints:
1<=Number of nodes<=100
Code;
#include<bits/stdc++.h>
struct Node {
int data;
Node *left;
Node *right;
Node(int val) {
data = val;
};
// Corner Case
return NULL;
vector<string> ip;
istringstream iss(str);
ip.push_back(str);
queue<Node*> queue;
queue.push(root);
int i = 1;
queue.pop();
if(currVal != "N") {
queue.push(currNode->left);
i++;
break;
currVal = ip[i];
queue.push(currNode->right);
i++;
return root;
int main()
int t;
scanf("%d ",&t);
while(t--)
string s;
getline(cin,s);
cout<<"True"<<endl;
else
cout<<"False"<<endl;
return 0;
/*
struct Node {
int data;
Node *left;
Node *right;
Node(int val) {
data = val;
};
*/
vector<int> v;
void inorder(Node* root)
if(root==NULL)
return;
inorder(root->left);
v.push_back(root->data);
inorder(root->right);
return;
return true;
if(!r1 || !r2)
return false;
{
return isSame(root,root);
}
Input:
1
/ \
2 3
Output: 2
Example 2:
Input:
2
\
1
/
3
Output: 3
Your Task:
You don't need to read input or print anything. Your task is to complete the function height() that takes root
Node of the Tree as input and returns the Height of the Tree. If the Tree is empty, return 0.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 <= Number of nodes <= 105
1 <= Data of a node <= 105
Code:
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left;
Node *right;
Node(int val) {
data = val;
};
// Corner Case
return NULL;
vector<string> ip;
istringstream iss(str);
ip.push_back(str);
// Create the root of the tree
queue<Node*> queue;
queue.push(root);
int i = 1;
queue.pop();
if(currVal != "N") {
queue.push(currNode->left);
i++;
break;
currVal = ip[i];
if(currVal != "N") {
queue.push(currNode->right);
i++;
return root;
int t;
scanf("%d ",&t);
while(t--)
string s;
getline(cin,s);
cout<<height(root)<<endl;
return 0;
struct Node {
int data;
Node *left;
Node *right;
Node(int val) {
data = val;
};*/
if(root==NULL)
return 0;
int lh=height(root->left);
int rh=height(root->right);
return (max(rh,lh)+1);
Example 1:
Input :
3
/ \
4 5
/ \
-10 4
Output: 16
Explanation :
Maximum Sum lies between leaf node 4 and 5.
4 + 4 + 3 + 5 = 16.
Example 2:
Input :
-15
/ \
5 6
/ \ / \
-8 1 3 9
/ \ \
2 -3 0
/ \
4 -1
/
10
Output : 27
Explanation:
The maximum possible sum from one leaf node
to another is (3 + 6 + 9 + 0 + -1 + 10 = 27)
Your Task:
You dont need to read input or print anything. Complete the function maxPathSum() which takes root node as
input parameter and returns the maximum sum between 2 leaf nodes.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of Tree)
Constraints:
1 ≤ N ≤ 10^4
Code:
#include <bits/stdc++.h>
// Tree Node
struct Node {
int data;
Node *left;
Node *right;
Node(int val) {
data = val;
};
// Corner Case
if (str.length() == 0 || str[0] == 'N') return NULL;
vector<string> ip;
istringstream iss(str);
queue.push(root);
int i = 1;
queue.pop();
// Get the current Node's value from the string
if (currVal != "N") {
queue.push(currNode->left);
i++;
currVal = ip[i];
if (currVal != "N") {
queue.push(currNode->right);
i++;
return root;
int main() {
int tc;
while (tc--) {
string treeString;
getline(cin, treeString);
return 0;
struct Node
int data;
Node(int x){
data = x;
};
*/
ll ans;
ll find(Node*root)
if(root==NULL)
return INT_MIN;
if(root->left==NULL&&root->right==NULL)
return root->data;
ll x=find(root->left);
ll y=find(root->right);
ans=max(ans,x+y+root->data);
return max(x,y)+root->data;
ans=INT_MIN;
find(root);
return ans;
}
Input:
1
/ \
2 3
Output: 3
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output: 4
Your Task:
You need to complete the function diameter() that takes node as parameter and returns the diameter.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Constraints:
1 <= Number of nodes <= 10000
1 <= Data of a node <= 1000
Code:
#include <bits/stdc++.h>
struct Node {
int data;
};
temp->data = val;
temp->left = NULL;
temp->right = NULL;
return temp;
// Corner Case
vector<string> ip;
istringstream iss(str);
queue<Node*> queue;
queue.push(root);
int i = 1;
queue.pop();
if (currVal != "N") {
currNode->left = newNode(stoi(currVal));
queue.push(currNode->left);
i++;
currVal = ip[i];
if (currVal != "N") {
currNode->right = newNode(stoi(currVal));
queue.push(currNode->right);
}
i++;
return root;
int main() {
int t;
scanf("%d\n", &t);
while (t--) {
string s;
getline(cin, s);
return 0;
int data;
Node(int x){
data = x;
}; */
class pair1{
public:
int height;
int diameter;
};
pair1 p;
if(root==NULL)
p.height=0;
p.diameter=0;
return p;
pair1 left=fastdiameter(root->left);
pair1 right=fastdiameter(root->right);
p.height=max(left.height,right.height)+1;
p.diameter=max(left.height+right.height+1,max(left.diameter,right.diameter));
return p;
pair1 p=fastdiameter(root);
return p.diameter;
struct Node
int data;
};
temp->data = val;
temp->left = NULL;
temp->right = NULL;
return temp;
// Corner Case
return NULL;
// Creating vector of strings from input
vector<string> ip;
istringstream iss(str);
ip.push_back(str);
queue<Node*> queue;
queue.push(root);
int i = 1;
queue.pop();
if(currVal != "N") {
currNode->left = newNode(stoi(currVal));
queue.push(currNode->left);
i++;
break;
currVal = ip[i];
if(currVal != "N") {
currNode->right = newNode(stoi(currVal));
queue.push(currNode->right);
}
i++;
return root;
int main()
int t;
scanf("%d ",&t);
while(t--)
string s;
getline(cin,s);
cout<< countLeaves(root)<<endl;
return 0;
struct Node
int data;
Node* left;
Node* right;
}; */
10
/ \
20 30 */
if(root==NULL)
return 0;
return 1;
return (countLeaves(root->left)+countLeaves(root->right));
Input:
1
/
2
\
3
Output: 0
Explanation: The max difference in height
of left subtree and right subtree is 2,
which is greater than 1. Hence unbalanced
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output: 1
Explanation: The max difference in height
of left subtree and right subtree is 1.
Hence balanced.
Your Task:
You don't need to take input. Just complete the function isBalanced() that takes root node as parameter and
returns true, if the tree is balanced else returns false.
Constraints:
1 <= Number of nodes <= 105
0 <= Data of a node <= 106
Expected time complexity: O(N)
Expected auxiliary space: O(h) , where h = height of tree
Code:
#include <bits/stdc++.h>
struct Node
int data;
};
temp->left = NULL;
temp->right = NULL;
return temp;
// Corner Case
return NULL;
vector<string> ip;
istringstream iss(str);
ip.push_back(str);
queue.push(root);
int i = 1;
queue.pop();
if(currVal != "N") {
currNode->left = newNode(stoi(currVal));
queue.push(currNode->left);
break;
currVal = ip[i];
if(currVal != "N") {
currNode->right = newNode(stoi(currVal));
queue.push(currNode->right);
i++;
return root;
int main()
{
int t;
scanf("%d ",&t);
while(t--)
string s;
getline(cin,s);
return 1;
struct Node
int data;
Node(int x){
data = x;
}
};
*/
if (!node)
return 0;
else
int l=height(node->left);
int r=height(node->right);
if (l>r) return(l+1);
else return(r+1);
return (isBalanced(root->right)&&isBalanced(root->left)&&abs(height(root->left)-height(root->right))<=1);