Skip to content

Commit 3449a4a

Browse files
committed
Longest consecutive sequence in a binary tree
1 parent cc666bb commit 3449a4a

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// { Driver Code Starts
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
// Tree Node
6+
struct Node
7+
{
8+
int data;
9+
Node* left;
10+
Node* right;
11+
};
12+
13+
// Utility function to create a new Tree Node
14+
Node* newNode(int val)
15+
{
16+
Node* temp = new Node;
17+
temp->data = val;
18+
temp->left = NULL;
19+
temp->right = NULL;
20+
21+
return temp;
22+
}
23+
24+
// Function to Build Tree
25+
Node* buildTree(string str)
26+
{
27+
// Corner Case
28+
if(str.length() == 0 || str[0] == 'N')
29+
return NULL;
30+
31+
// Creating vector of strings from input
32+
// string after spliting by space
33+
vector<string> ip;
34+
35+
istringstream iss(str);
36+
for(string str; iss >> str; )
37+
ip.push_back(str);
38+
39+
// Create the root of the tree
40+
Node* root = newNode(stoi(ip[0]));
41+
42+
// Push the root to the queue
43+
queue<Node*> queue;
44+
queue.push(root);
45+
46+
// Starting from the second element
47+
int i = 1;
48+
while(!queue.empty() && i < ip.size()) {
49+
50+
// Get and remove the front of the queue
51+
Node* currNode = queue.front();
52+
queue.pop();
53+
54+
// Get the current node's value from the string
55+
string currVal = ip[i];
56+
57+
// If the left child is not null
58+
if(currVal != "N") {
59+
60+
// Create the left child for the current node
61+
currNode->left = newNode(stoi(currVal));
62+
63+
// Push it to the queue
64+
queue.push(currNode->left);
65+
}
66+
67+
// For the right child
68+
i++;
69+
if(i >= ip.size())
70+
break;
71+
currVal = ip[i];
72+
73+
// If the right child is not null
74+
if(currVal != "N") {
75+
76+
// Create the right child for the current node
77+
currNode->right = newNode(stoi(currVal));
78+
79+
// Push it to the queue
80+
queue.push(currNode->right);
81+
}
82+
i++;
83+
}
84+
85+
return root;
86+
}
87+
88+
89+
// } Driver Code Ends
90+
/*
91+
struct Node
92+
{
93+
int data;
94+
Node *left, *right;
95+
};
96+
*/
97+
class Solution
98+
{
99+
public:
100+
// your task is tp complete this function
101+
// function should return the length of the longestConsecutive
102+
// sequence
103+
void longestConsecutiveSequence(Node* root, int count, int prevVal, int &res)
104+
{
105+
if(!root)
106+
return;
107+
if(root->data == prevVal + 1)
108+
{
109+
count++;
110+
res = max(res, count);
111+
}
112+
else
113+
count = 1;
114+
longestConsecutiveSequence(root->left, count, root->data, res);
115+
longestConsecutiveSequence(root->right, count, root->data, res);
116+
}
117+
int longestConsecutive(Node* root)
118+
{
119+
//Code here
120+
int res = 1, prevVal = root->data, count = 1;
121+
longestConsecutiveSequence(root, count, prevVal, res);
122+
return res == 1 ? -1 : res;
123+
}
124+
};
125+
126+
// { Driver Code Starts.
127+
int main()
128+
{
129+
int t;
130+
cin>>t;
131+
getchar();
132+
while (t--)
133+
{
134+
string s;
135+
getline(cin,s);
136+
Node* root = buildTree(s);
137+
Solution obj;
138+
cout<<obj.longestConsecutive(root)<<endl;
139+
}
140+
return 0;
141+
}
142+
// } Driver Code Ends

Some_More_Trees/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@
1313
/ \ => / \
1414
3 2 2 3
1515
The inorder of mirror is 2 1 3
16+
17+
## 2. Longest Consecutive Sequence in a Binary Tree:
18+
Given a Binary Tree find the length of the longest path which comprises of connected nodes with consecutive values in increasing order.
19+
Example 1:
20+
Input :
21+
1
22+
/ \
23+
2 3
24+
Output: 2
25+
Explanation : Longest sequence is 1, 2.

0 commit comments

Comments
 (0)