Skip to content

Commit 97df423

Browse files
committed
0563 solved.
1 parent 8aef525 commit 97df423

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
project(cpp_0563)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_0563 main2.cpp)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// Source : https://leetcode.com/problems/binary-tree-tilt/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-02-23
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
10+
/// Recursive Simulation
11+
/// Time Complexity: O(n^2)
12+
/// Space Complexity: O(h)
13+
14+
/// Definition for a binary tree node.
15+
struct TreeNode {
16+
int val;
17+
TreeNode *left;
18+
TreeNode *right;
19+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
20+
};
21+
22+
23+
class Solution {
24+
25+
public:
26+
int findTilt(TreeNode* root) {
27+
28+
if(!root) return 0;
29+
return abs(sum(root->left) - sum(root->right)) + findTilt(root->left) + findTilt(root->right);
30+
}
31+
32+
private:
33+
int sum(TreeNode* root){
34+
35+
if(!root) return 0;
36+
return root->val + sum(root->left) + sum(root->right);
37+
}
38+
};
39+
40+
41+
int main() {
42+
43+
return 0;
44+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/// Source : https://leetcode.com/problems/binary-tree-tilt/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-02-23
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
10+
/// Recursive and use class variable to record the result
11+
/// Time Complexity: O(n)
12+
/// Space Complexity: O(h)
13+
14+
/// Definition for a binary tree node.
15+
struct TreeNode {
16+
int val;
17+
TreeNode *left;
18+
TreeNode *right;
19+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
20+
};
21+
22+
23+
class Solution {
24+
25+
private:
26+
int result;
27+
28+
public:
29+
int findTilt(TreeNode* root) {
30+
31+
result = 0;
32+
dfs(root);
33+
return result;
34+
}
35+
36+
private:
37+
int dfs(TreeNode* root){
38+
39+
if(!root) return 0;
40+
int left = dfs(root->left);
41+
int right = dfs(root->right);
42+
result += abs(left - right);
43+
return root->val + left + right;
44+
}
45+
};
46+
47+
48+
int main() {
49+
50+
return 0;
51+
}

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
396396
| | | | | | |
397397
| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/description/) | [solution](https://leetcode.com/problems/array-partition-i/solution/) | [C++](0561-Array-Partition-I/cpp-0561/) | | |
398398
| | | | | | |
399+
| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [solution](https://leetcode.com/problems/binary-tree-tilt/solution/) | [C++](0563-Binary-Tree-Tilt/cpp-0563/) | | |
400+
| | | | | | |
399401
| 572 | [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/description/) | [solution](https://leetcode.com/problems/subtree-of-another-tree/solution/) | [C++](0572-Subtree-of-Another-Tree/cpp-0572/) | | |
400402
| | | | | | |
401403
| 583 | [Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings/description/) | [solution](https://leetcode.com/problems/delete-operation-for-two-strings/solution/) | [C++](0583-Delete-Operation-for-Two-Strings/cpp-0583/) | | |

0 commit comments

Comments
 (0)