Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dbf7eb9

Browse files
Create Longest Leaf to root path
1 parent fed003f commit dbf7eb9

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

‎Test3/Longest Leaf to root path

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Following is the Binary Tree node structure
2+
/**************
3+
class BinaryTreeNode {
4+
public :
5+
T data;
6+
BinaryTreeNode<T> *left;
7+
BinaryTreeNode<T> *right;
8+
9+
BinaryTreeNode(T data) {
10+
this -> data = data;
11+
left = NULL;
12+
right = NULL;
13+
}
14+
};
15+
***************/
16+
17+
vector<int>* longestPath(BinaryTreeNode<int>* root) {
18+
if(root==NULL){
19+
return NULL;
20+
}
21+
if(root->left==NULL&&root->right==NULL){
22+
vector<int>* ans=new vector<int>();
23+
ans->push_back(root->data);
24+
return ans;
25+
}
26+
vector<int>* leftans=longestPath(root->left);
27+
vector<int>* rightans=longestPath(root->right);
28+
if(!leftans){
29+
rightans->push_back(root->data);
30+
return rightans;
31+
}
32+
else if(!rightans){
33+
leftans->push_back(root->data);
34+
return leftans;
35+
}
36+
else{
37+
if(leftans->size()>rightans->size()){
38+
leftans->push_back(root->data);
39+
delete rightans;
40+
return leftans;
41+
}
42+
else{
43+
rightans->push_back(root->data);
44+
delete leftans;
45+
return rightans;
46+
}
47+
}
48+
49+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /