
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Binary Tree Longest Consecutive Sequence in C++
Suppose we have a binary tree; we have to check whether we can find the length of the longest consecutive sequence path. If the path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to follow parent to child but not reverse.
So, if the input is like,
then the output will be 3, as the Longest consecutive sequence path is 3-4-5, so return 3.
To solve this, we will follow these steps −
Define a function solveUtil(), this will take node, prev, len initialize it with 1,
if node is null, then −
return
if prev + 1 is same as val of node, then −
(increase len by 1)
ans := maximum of ans and len
solveUtil(left of node, val of node, len)
solveUtil(right of node, val of node, len)
Otherwise
solveUtil(left of node, val of node, 1)
solveUtil(right of node, val of node, 1)
Define a function solve(), this will take A,
ans := 1
solveUtil(A, -infinity)
return ans
From the main method do the following −
if root is null, then −
return 0
return solve(root)
Example
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; class TreeNode{ public: int val; TreeNode *left, *right; TreeNode(int data){ val = data; left = NULL; right = NULL; } }; void insert(TreeNode **root, int val){ queue<TreeNode*> q; q.push(*root); while(q.size()){ TreeNode *temp = q.front(); q.pop(); if(!temp->left){ if(val != NULL) temp->left = new TreeNode(val); else temp->left = new TreeNode(0); return; }else{ q.push(temp->left); } if(!temp->right){ if(val != NULL) temp->right = new TreeNode(val); else temp->right = new TreeNode(0); return; }else{ q.push(temp->right); } } } TreeNode *make_tree(vector<int< v){ TreeNode *root = new TreeNode(v[0]); for(int i = 1; i<v.size(); i++){ insert(&root, v[i]); } return root; } class Solution { public: int ans; void solveUtil(TreeNode* node, int prev, int len = 1){ if (!node) return; if (prev + 1 == node->val) { len++; ans = max(ans, len); solveUtil(node->left, node->val, len); solveUtil(node->right, node->val, len); } else { solveUtil(node->left, node->val, 1); solveUtil(node->right, node->val, 1); } } int solve(TreeNode* A){ ans = 1; solveUtil(A, INT_MIN); return ans; } int longestConsecutive(TreeNode* root){ if (!root) return 0; return solve(root); } }; main(){ Solution ob; TreeNode *root = new TreeNode(1); root->right = new TreeNode(3); root->right->left = new TreeNode(2); root->right->right = new TreeNode(4); root->right->right->right = new TreeNode(5); cout << (ob.longestConsecutive(root)); }
Input
TreeNode *root = new TreeNode(1); root->right = new TreeNode(3); root->right->left = new TreeNode(2); root->right->right = new TreeNode(4); root->right->right->right = new TreeNode(5);
Output
3
- Related Articles
- Binary Tree Longest Consecutive Sequence II in C++
- Longest Consecutive Sequence in Python
- Program to find length of longest consecutive path of a binary tree in python
- Program to find length of longest consecutive sequence in Python
- Length of the longest possible consecutive sequence of numbers in JavaScript
- Longest ZigZag Path in a Binary Tree in C++
- Verify Preorder Sequence in Binary Search Tree in C++
- Maximum Consecutive Increasing Path Length in Binary Tree in C++
- Find longest sequence of 1’s in binary representation with one flip in C++
- Longest Arithmetic Sequence in C++
- Program to find longest consecutive run of 1s in binary form of n in Python
- Program to find longest even value path of a binary tree in Python
- Finding longest consecutive joins in JavaScript
- Program to find longest consecutive run of 1 in binary form of a number in C++
- Program to find length of longest alternating path of a binary tree in python
