forked from wangcy6/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path011_tree_finx_max_subtree.cpp
86 lines (72 loc) · 1.91 KB
/
011_tree_finx_max_subtree.cpp
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
// C++ program to find largest subtree
// sum in a given binary tree.
#include <bits/stdc++.h>
using namespace std;
// Structure of a tree node.
struct Node {
int key;
Node *left, *right;
};
// Function to create new tree node.
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return temp;
}
// Helper function to find largest
// subtree sum recursively.
int findLargestSubtreeSumUtil(Node* root, int& ans)
{
// If current node is null then
// return 0 to parent node.
if (root == NULL)
return 0;
// Subtree sum rooted at current node.
int currSum = root->key +
findLargestSubtreeSumUtil(root->left, ans)
+ findLargestSubtreeSumUtil(root->right, ans);
// Update answer if current subtree
// sum is greater than answer so far.
ans = max(ans, currSum);
// Return current subtree sum to
// its parent node.
return currSum;
}
// Function to find largest subtree sum.
int findLargestSubtreeSum(Node* root)
{
// If tree does not exist,
// then answer is 0.
if (root == NULL)
return 0;
// Variable to store maximum subtree sum.
int ans = INT_MIN;
// Call to recursive function to
// find maximum subtree sum.
findLargestSubtreeSumUtil(root, ans);
return ans;
}
// Driver function
int main()
{
/*
1
/ \
/ \
-2 3
/ \ / \
/ \ / \
4 5 -6 2
*/
Node* root = newNode(1);
root->left = newNode(-2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(-6);
root->right->right = newNode(2);
cout << findLargestSubtreeSum(root);
return 0;
}