-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdp_on_trees.cpp
More file actions
146 lines (123 loc) · 3.54 KB
/
dp_on_trees.cpp
File metadata and controls
146 lines (123 loc) · 3.54 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Problems on DP on trees
// Format of any tree solution will be as follows:
// 1. Base case
// 2. Hypothesis Part -> calling function for left and right subtrees.
// 3. Induction Part -> calculation at the node and the return statement.
// 1. Diameter of a Binary Tree
// 2. Maximum path sum from any node to any node
// 3. Maximum Path sum from leaf to leaf
// 4. Diamerter of N-ary tree.
#include <iostream>
#include <climits>
using namespace std;
class Node
{
public:
int val = 0;
Node *left = NULL;
Node *right = NULL;
};
// Problem statement:
// For any give tree find its diameter.
// Diameter -> length of longest path, path may not pass through root
// Algorithm:
// 1. At every node we have to decide whether path with node is answer or the path with its parent is answer.
// 2. We will maintain two variables, res storing the actual answer, and temp whose value will be returned if parent will be the answer.
// Brute Force approach
int heightBT(Node *root)
{
// Base case
if (!root)
return 0;
if (!root->left && !root->right)
return 0;
int h_l = heightBT(root->left);
int h_r = heightBT(root->right);
return 1 + max(h_l, h_r);
}
int binary_tree_diameter_brute(Node *root)
{
// Base case: root is None
if (!root)
return 0;
int curr_max = 0, h_l = 0, h_r = 0;
if (root->left)
curr_max += (heightBT(root->left) + 1);
if (root->right)
curr_max += (heightBT(root->right) + 1);
h_l = binary_tree_diameter_brute(root->left);
h_r = binary_tree_diameter_brute(root->right);
return max(curr_max, h_l, h_r);
}
int binary_tree_diameter(Node *root, int &res)
{
// Base case
if (!root)
return 0;
// Hypothesis
int ld = binary_tree_diameter(root->left, res);
int rd = binary_tree_diameter(root->right, res);
// Induction
// 1. If current node is the answer
int ans = ld + rd + 1;
res = max(ans, res);
// 2. If Parent is the answer
int temp = max(ld, rd) + 1;
return temp;
}
// Problem statement:
// For any give tree find maximum path sum,
// Path may not pass from root and node may have negative value.
// Algorithm:
// At every sub tree we have two possibilities, Whether path involves parent or not.
// For two possibilities we have 1 and 3 cases respectively.
// Val of node is max.
// Val of node + val of left child is max.
// Val of node + val of right child is max.
// Val of node + val of left + right child is max.
int valueBt(Node *root)
{
if (!root)
return INT_MIN;
int lv = INT_MIN;
if (root->left)
lv = valueBt(root->left);
int rv = INT_MIN;
if (root->right)
rv = valueBt(root->right);
return max(max(lv, rv) + root->val, root->val, lv + rv + root->val);
}
int max_path_sum_brute(Node *root)
{
// Base case: root is None
if (!root)
return 0;
int h_l = INT_MIN, h_r = INT_MIN;
h_l = binary_tree_diameter_brute(root->left);
h_r = binary_tree_diameter_brute(root->right);
int curr_max = valueBt(root);
return max(curr_max, h_l, h_r);
}
int solve(Node *root, int &res)
{
// Base case
if (!root)
return 0;
// Hypothesis
int lv = solve(root->left, res);
int rv = solve(root->right, res);
// Induction
int topa = max(max(lv, rv) + root->val, root->val);
int ans = max(topa, lv + rv + root->val);
res = max(ans, res);
return topa;
}
int max_path_sum(Node *root)
{
int res = INT_MIN;
solve(root, res);
return res;
}
int main()
{
}