Skip to content

Commit e51f4c7

Browse files
🌴 Day 8
1 parent a299b26 commit e51f4c7

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
7. [Add Two Numbers II](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/564/week-1-november-1st-november-7th/3521/) ➡️ [CPP Solution](Week1/addTwoNumbers.cpp)
1515

1616
## Week 2 🚧
17-
Coming soon...
17+
1. [Binary Tree Tilt](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/565/week-2-november-8th-november-14th/3524/) ➡️ [CPP Solution](Week2/findTilt.cpp)
1818

1919
## Week 3 🚧
2020
Coming soon...

Week2/findTilt.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
private:
14+
int tilt(TreeNode* root, int& result) {
15+
if(root == NULL) return 0;
16+
17+
int left = tilt(root->left, result);
18+
int right = tilt(root->right, result);
19+
20+
result += abs(left - right);
21+
22+
return left + right + root->val;
23+
}
24+
public:
25+
int findTilt(TreeNode* root) {
26+
int result = 0;
27+
tilt(root, result);
28+
return result;
29+
}
30+
};

0 commit comments

Comments
 (0)