-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCS_46_ConvertABinaryTreeToItsSumTree.cpp
74 lines (65 loc) · 1.59 KB
/
CS_46_ConvertABinaryTreeToItsSumTree.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
#include <bits/stdc++.h>
using namespace std;
template <typename T = int>
class TreeNode
{
public:
T data;
TreeNode<T> *left;
TreeNode<T> *right;
TreeNode(T val)
{
this->data = val;
left = NULL;
right = NULL;
}
};
int sum(TreeNode<int> *root)
{
if (!root)
return 0;
if (!root->left && !root->right)
{
// leaf node
int leafVal = root->data;
root->data = 0;
return leafVal;
}
int lsum = sum(root->left);
int rsum = sum(root->right);
int currNodeVal = root->data; // store the current node val as it'll be overwritten
root->data = lsum + rsum; // update current node val to its subtrees' sum
return root->data + currNodeVal; // return previous val of curr node + new sum val of curr node
}
// tc: O(n) & sc: O(n)
TreeNode<int> *convertToSumTree(TreeNode<int> *root)
{
sum(root);
return root;
}
void printTree(TreeNode<int> *root)
{
if (!root)
return;
cout << root->data << " ";
printTree(root->left);
printTree(root->right);
}
int main()
{
TreeNode<int> *root = new TreeNode<int>(10);
root->left = new TreeNode<int>(-2);
root->right = new TreeNode<int>(6);
root->left->left = new TreeNode<int>(8);
root->left->right = new TreeNode<int>(-4);
root->right->left = new TreeNode<int>(7);
root->right->right = new TreeNode<int>(5);
cout << "Original Tree (Preorder): ";
printTree(root);
cout << endl;
cout << "Sum Tree: (Preorder): ";
convertToSumTree(root);
printTree(root);
cout << endl;
return 0;
}