-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmax_sum_path.cpp
More file actions
127 lines (104 loc) · 2.58 KB
/
max_sum_path.cpp
File metadata and controls
127 lines (104 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// Created by Mayank Parasar on 2020-04-05.
//
/*
* Given a binary tree, find and return the largest path from root to leaf.
Here's an example and some starter code:
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
def largest_path_sum(tree):
# Fill this in.
tree = Node(1)
tree.left = Node(3)
tree.right = Node(2)
tree.right.left = Node(4)
tree.left.right = Node(5)
# 1
# / \
# 3 2
# \ /
# 5 4
print(largest_path_sum(tree))
# [1, 3, 5]
*/
#include <iostream>
// #include <map>
#include <vector>
using namespace std;
struct node {
int val;
node* left;
node* right;
node(int x) : val(x) {
left = nullptr;
right = nullptr;
}
};
vector<pair<int, vector<int>>> path_traversal;
void tree_traversal(node* node_, int sum, vector<int> visited_nodes) {
if(node_ == nullptr) {
return;
}
else if(node_->left == nullptr &&
node_->right == nullptr) {
sum += node_->val;
visited_nodes.push_back(node_->val);
// this is leaf node
path_traversal.push_back(make_pair(sum, visited_nodes));
return;
}
else {
// do the traversal..
if(node_->left != nullptr) {
sum += node_->val;
visited_nodes.push_back(node_->val);
tree_traversal(node_->left, sum, visited_nodes);
// undo change
sum -= node_->val;
visited_nodes.pop_back();
}
if(node_->right != nullptr) {
sum += node_->val;
visited_nodes.push_back(node_->val);
tree_traversal(node_->right, sum, visited_nodes);
// undo change
sum -= node_->val;
visited_nodes.pop_back();
}
}
return;
}
int main() {
node* node1 = new node(1);
node* node2 = new node(2);
node* node3 = new node(3);
node* node4 = new node(4);
node* node5 = new node(5);
// connect them
node1->left = node3;
node1->right = node2;
node3->right = node5;
node2->left = node4;
vector<int> path;
tree_traversal(node1, 0, path);
int max = -1;
// find the vector with maximum sum val.
for(auto i : path_traversal) {
cout << i.first << ": ";
if( (int)i.first > max) {
max = i.first;
path = i.second;
}
for(auto k : i.second) {
cout << k << ", ";
}
cout << endl;
}
cout << "maximum sum path is:" << endl;
for(auto i : path)
cout << i << " ";
return 0;
}