GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

Longest path in a tree with just one bend. May or may not start with from the root.

Source of the question - http://www.geeksforgeeks.org/amazon-interview-set-54-on-campus-for-sde/

It would be very helpful if somebody can throw some light on this question.

asked Aug 19, 2015 by codersx

Question is not clear to me. I myself cannot figure out what he meant by bend?

The question I am asking here is from the Amazon Set 54 . I think the bent means that in a path you can switch at max one time to the left . For e.g. LRRRRRRRR......R ,RLLLLLL....L , RRRRRR........R, LLLLLLL.....L, these sequence are allowed but the sequence such as LRLRLRLRLLLRRR , RLLLRRRLLRR are not allowed.

L=>Left

R=> Right

 

2 Answers

This finds the length of the longest path starting from root or any other node with just one bent downwards. You are happily invited to comment bugs, if any :)

​
// Author :: Gaurav Ahirwar
#include<bits/stdc++.h>
using namespace std;

struct node 
{	
    int data;
    int pathlen;
    struct node* left, *right;
};
typedef struct node node;

node *newNode(int data)
{
    node *temp = (node *)malloc(sizeof(struct node));
    temp->data  = data;
    temp->pathlen = 0;
    temp->left  = temp->right = NULL;
    return(temp);
}

void getmaxval(node *root, int &res) {
	if(!root) return;	
	res = max(res, root->pathlen);
	getmaxval(root->left, res);
	getmaxval(root->right,res);
}

int getlength(node *root, char bend, bool flag) {
	if(!root) return 0;	
	int ans1 = 0, ans2 = 0;
	
	if(flag) ans1 = bend ==	'r' ? 1 + getlength(root->right, bend, flag) : 1 + getlength(root->left, bend, flag);
	else {
		if(bend == 'r') ans1 = 1 + max(getlength(root->right, bend, flag), getlength(root->left, 'l', !flag));
		if(bend == 'l') ans1 = 1 + max(getlength(root->left, bend, flag), getlength(root->right, 'r', !flag));		
	}
	
	root->pathlen = 1 + max(getlength(root->left, 'l', false), getlength(root->right, 'r', false));
	return ans1;
}

int solve(node *root) {
	if(root == NULL) return 0;
	root->pathlen = 1 + max(getlength(root->left, 'l', false), getlength(root->right, 'r', false));
}
  

int main() {

// Let us construct the BT shown in the below figure
    node *root  	  = newNode(4);
    root->left        = newNode(2);
    root->right       = newNode(6);
    root->left->left  = newNode(1);
    root->left->right = newNode(3);
    root->right->left  = newNode(5);
    root->right->right = newNode(7);
    root->right->right->left = newNode(9);
	root->right->right->left->left = newNode(12);
	root->right->right->left->right = newNode(10);
	root->right->right->left->right->right = newNode(11);
	root->right->right->left->right->right->right = newNode(13);
	root->right->right->left->right->right->right->right = newNode(14);	
 	/* Constructed binary tree is 
            4
          /   \
        2      6
      /  \	  /	\
    1     3  5  7
               /
	          9
	        / \
	       12 10
	            \
	             11
	              \
	              13
	               \
	               14
  
  */
	
	solve(root);
	int res = 0;
	getmaxval(root, res);
	cout << res << endl;
 	return 0;
}
​

 

We can infact modify the code above to return longest path with at most k bents, check the below solution.. :)
 

// Author :: Gaurav Ahirwar
#include<bits/stdc++.h>
using namespace std;

int x;
struct node 
{	
    int data;
    int pathlen;
    struct node* left, *right;
};
typedef struct node node;

node *newNode(int data)
{
    node *temp = (node *)malloc(sizeof(struct node));
    temp->data  = data;
    temp->pathlen = 0;
    temp->left  = temp->right = NULL;
    return(temp);
}

void getmaxval(node *root, int &res) {
	if(!root) return;	
	res = max(res, root->pathlen);
	getmaxval(root->left, res);
	getmaxval(root->right,res);
}

void levelorder(node *root) {
	if(!root) {
		cout << "Tree Empty\n";
		return;
	}
	
	queue<node *> Q;
	Q.push(root);
	while(!Q.empty()) {
		int n = Q.size();
		while(n-- != 0) {
			node *temp = Q.front();
			cout << temp->pathlen << " ";
			if(temp->left) Q.push(temp->left);
			if(temp->right) Q.push(temp->right);
			Q.pop();
		}
		cout << endl;
	}
	cout << endl;
}

int getlength(node *root, char bend, int k) {
	if(!root) return 0;	
	int ans1 = 0, ans2 = 0;
	
	if(k == 0) ans1 = bend ==	'r' ? 1 + getlength(root->right, bend, k) : 1 + getlength(root->left, bend, k);
	else {
		if(bend == 'r') ans1 = 1 + max(getlength(root->right, bend, k), getlength(root->left, 'l', k-1));
		if(bend == 'l') ans1 = 1 + max(getlength(root->left, bend, k), getlength(root->right, 'r', k-1));		
	}
	
	root->pathlen = 1 + max(getlength(root->left, 'l', x), getlength(root->right, 'r', x));
	return ans1;
}

int solve(node *root, int k) {
	if(root == NULL) return 0;
	root->pathlen = 1 + max(getlength(root->left, 'l', k), getlength(root->right, 'r', k));
}
  

int main() {

// Let us construct the BT shown in the below figure
    node *root  	  = newNode(4);
    root->left        = newNode(2);
    root->right       = newNode(6);
    root->left->left  = newNode(1);
    root->left->right = newNode(3);
    root->right->left  = newNode(5);
    root->right->right = newNode(7);
    root->right->right->left = newNode(9);
	root->right->right->left->left = newNode(12);
	root->right->right->left->right = newNode(10);
	root->right->right->left->right->right = newNode(11);
	root->right->right->left->right->right->right = newNode(13);
	root->right->right->left->right->right->right->right = newNode(14);	
 	/* Constructed binary tree is 
            4
          /   \
        2      6
      /  \	  /	\
    1     3  5  7
               /
	          9
	        / \
	       12 10
	            \
	             11
	              \
	              13
	               \
	               14
  
  */
	
	int k = 2;
	x = k;
	solve(root, k);
	int res = 0;
	getmaxval(root, res);
	cout << "Pathlen = " << res << endl;
//	levelorder(root);
 	return 0;
}

 

answered Aug 20, 2015 by Mr.Lazy

Hello Gaurav , 

Can you add little bit explaination for the code ....

Thanks in advance...!!

answered Aug 20, 2015 by Abhishek Somani
edited Aug 20, 2015 by Abhishek Somani

Note: This question is different from the one which mentioned in the quora link you posted. Here,  path may or may not start with root. If root, was the only case, than there is no fun in solving this question. Itne jaldi me kyu rehta hai tu :P

...