-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathBinary Tree Tilt.cpp
48 lines (38 loc) · 1.6 KB
/
Binary Tree Tilt.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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
/* We will take help of recursion
Each function call will return the sum of subtree with root as current root
and the tilt sum will be updated in the variable passed as reference
*/
int getTiltSum(TreeNode* root, int &tiltSum){
if(root == NULL){
return 0;
}
if(root->left == NULL and root->right == NULL){ // If we have a leaf
tiltSum+=0; // Add nothing to tilt sum
return root->val; // return its value as sum
}
int leftSubTreeSum = getTiltSum(root->left, tiltSum); // Sum of all nodes in left
int rightSubTreeSum = getTiltSum(root->right, tiltSum); // and right subtrees
tiltSum += abs(leftSubTreeSum - rightSubTreeSum); // Update the tilt sum
// return the sum of left Subtree + right subtree + current node as the sum of the
return (root->val + leftSubTreeSum + rightSubTreeSum); // subtree with root as current node.
}
int findTilt(TreeNode* root) {
// variable to be passed as refrence and store the result
int tiltSum = 0;
getTiltSum(root, tiltSum);
return tiltSum;
}
};