Skip to content

Commit f713d1b

Browse files
committed
feat: add solutions to lc problem: No.0563
No.0563.Binary Tree Tilt
1 parent d1ef183 commit f713d1b

File tree

6 files changed

+346
-16
lines changed

6 files changed

+346
-16
lines changed

solution/0500-0599/0563.Binary Tree Tilt/README.md

+123-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
<li><code>-1000 <= Node.val <= 1000</code></li>
5858
</ul>
5959

60-
6160
## 解法
6261

6362
<!-- 这里可写通用的实现逻辑 -->
@@ -69,15 +68,137 @@
6968
<!-- 这里可写当前语言的特殊实现逻辑 -->
7069

7170
```python
72-
71+
# Definition for a binary tree node.
72+
# class TreeNode:
73+
# def __init__(self, val=0, left=None, right=None):
74+
# self.val = val
75+
# self.left = left
76+
# self.right = right
77+
class Solution:
78+
def findTilt(self, root: TreeNode) -> int:
79+
ans = 0
80+
81+
def sum(root):
82+
if root is None:
83+
return 0
84+
nonlocal ans
85+
left = sum(root.left)
86+
right = sum(root.right)
87+
ans += abs(left - right)
88+
return root.val + left + right
89+
90+
sum(root)
91+
return ans
7392
```
7493

7594
### **Java**
7695

7796
<!-- 这里可写当前语言的特殊实现逻辑 -->
7897

7998
```java
99+
/**
100+
* Definition for a binary tree node.
101+
* public class TreeNode {
102+
* int val;
103+
* TreeNode left;
104+
* TreeNode right;
105+
* TreeNode() {}
106+
* TreeNode(int val) { this.val = val; }
107+
* TreeNode(int val, TreeNode left, TreeNode right) {
108+
* this.val = val;
109+
* this.left = left;
110+
* this.right = right;
111+
* }
112+
* }
113+
*/
114+
class Solution {
115+
private int ans;
116+
117+
public int findTilt(TreeNode root) {
118+
ans = 0;
119+
sum(root);
120+
return ans;
121+
}
122+
123+
private int sum(TreeNode root) {
124+
if (root == null) {
125+
return 0;
126+
}
127+
int left = sum(root.left);
128+
int right = sum(root.right);
129+
ans += Math.abs(left - right);
130+
return root.val + left + right;
131+
}
132+
}
133+
```
134+
135+
### **C++**
136+
137+
```cpp
138+
/**
139+
* Definition for a binary tree node.
140+
* struct TreeNode {
141+
* int val;
142+
* TreeNode *left;
143+
* TreeNode *right;
144+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
145+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
146+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
147+
* };
148+
*/
149+
class Solution {
150+
public:
151+
int ans;
152+
153+
int findTilt(TreeNode* root) {
154+
ans = 0;
155+
sum(root);
156+
return ans;
157+
}
158+
159+
int sum(TreeNode* root) {
160+
if (!root) return 0;
161+
int left = sum(root->left), right = sum(root->right);
162+
ans += abs(left - right);
163+
return root->val + left + right;
164+
}
165+
};
166+
```
80167
168+
### **Go**
169+
170+
```go
171+
/**
172+
* Definition for a binary tree node.
173+
* type TreeNode struct {
174+
* Val int
175+
* Left *TreeNode
176+
* Right *TreeNode
177+
* }
178+
*/
179+
var ans int
180+
181+
func findTilt(root *TreeNode) int {
182+
ans = 0
183+
sum(root)
184+
return ans
185+
}
186+
187+
func sum(root *TreeNode) int {
188+
if root == nil {
189+
return 0
190+
}
191+
left, right := sum(root.Left), sum(root.Right)
192+
ans += abs(left - right)
193+
return root.Val + left + right
194+
}
195+
196+
func abs(x int) int {
197+
if x > 0 {
198+
return x
199+
}
200+
return -x
201+
}
81202
```
82203

83204
### **...**

solution/0500-0599/0563.Binary Tree Tilt/README_EN.md

+123-2
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,142 @@ Sum of every tilt : 0 + 0 + 0 + 2 + 7 + 6 = 15
5151
<li><code>-1000 &lt;= Node.val &lt;= 1000</code></li>
5252
</ul>
5353

54-
5554
## Solutions
5655

5756
<!-- tabs:start -->
5857

5958
### **Python3**
6059

6160
```python
62-
61+
# Definition for a binary tree node.
62+
# class TreeNode:
63+
# def __init__(self, val=0, left=None, right=None):
64+
# self.val = val
65+
# self.left = left
66+
# self.right = right
67+
class Solution:
68+
def findTilt(self, root: TreeNode) -> int:
69+
ans = 0
70+
71+
def sum(root):
72+
if root is None:
73+
return 0
74+
nonlocal ans
75+
left = sum(root.left)
76+
right = sum(root.right)
77+
ans += abs(left - right)
78+
return root.val + left + right
79+
80+
sum(root)
81+
return ans
6382
```
6483

6584
### **Java**
6685

6786
```java
87+
/**
88+
* Definition for a binary tree node.
89+
* public class TreeNode {
90+
* int val;
91+
* TreeNode left;
92+
* TreeNode right;
93+
* TreeNode() {}
94+
* TreeNode(int val) { this.val = val; }
95+
* TreeNode(int val, TreeNode left, TreeNode right) {
96+
* this.val = val;
97+
* this.left = left;
98+
* this.right = right;
99+
* }
100+
* }
101+
*/
102+
class Solution {
103+
private int ans;
104+
105+
public int findTilt(TreeNode root) {
106+
ans = 0;
107+
sum(root);
108+
return ans;
109+
}
110+
111+
private int sum(TreeNode root) {
112+
if (root == null) {
113+
return 0;
114+
}
115+
int left = sum(root.left);
116+
int right = sum(root.right);
117+
ans += Math.abs(left - right);
118+
return root.val + left + right;
119+
}
120+
}
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
/**
127+
* Definition for a binary tree node.
128+
* struct TreeNode {
129+
* int val;
130+
* TreeNode *left;
131+
* TreeNode *right;
132+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
133+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
134+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
135+
* };
136+
*/
137+
class Solution {
138+
public:
139+
int ans;
140+
141+
int findTilt(TreeNode* root) {
142+
ans = 0;
143+
sum(root);
144+
return ans;
145+
}
146+
147+
int sum(TreeNode* root) {
148+
if (!root) return 0;
149+
int left = sum(root->left), right = sum(root->right);
150+
ans += abs(left - right);
151+
return root->val + left + right;
152+
}
153+
};
154+
```
68155
156+
### **Go**
157+
158+
```go
159+
/**
160+
* Definition for a binary tree node.
161+
* type TreeNode struct {
162+
* Val int
163+
* Left *TreeNode
164+
* Right *TreeNode
165+
* }
166+
*/
167+
var ans int
168+
169+
func findTilt(root *TreeNode) int {
170+
ans = 0
171+
sum(root)
172+
return ans
173+
}
174+
175+
func sum(root *TreeNode) int {
176+
if root == nil {
177+
return 0
178+
}
179+
left, right := sum(root.Left), sum(root.Right)
180+
ans += abs(left - right)
181+
return root.Val + left + right
182+
}
183+
184+
func abs(x int) int {
185+
if x > 0 {
186+
return x
187+
}
188+
return -x
189+
}
69190
```
70191

71192
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
public:
14+
int ans;
15+
16+
int findTilt(TreeNode* root) {
17+
ans = 0;
18+
sum(root);
19+
return ans;
20+
}
21+
22+
int sum(TreeNode* root) {
23+
if (!root) return 0;
24+
int left = sum(root->left), right = sum(root->right);
25+
ans += abs(left - right);
26+
return root->val + left + right;
27+
}
28+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
var ans int
10+
11+
func findTilt(root *TreeNode) int {
12+
ans = 0
13+
sum(root)
14+
return ans
15+
}
16+
17+
func sum(root *TreeNode) int {
18+
if root == nil {
19+
return 0
20+
}
21+
left, right := sum(root.Left), sum(root.Right)
22+
ans += abs(left - right)
23+
return root.Val + left + right
24+
}
25+
26+
func abs(x int) int {
27+
if x > 0 {
28+
return x
29+
}
30+
return -x
31+
}

solution/0500-0599/0563.Binary Tree Tilt/Solution.java

+20-12
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,31 @@
44
* int val;
55
* TreeNode left;
66
* TreeNode right;
7-
* TreeNode(int x) { val = x; }
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
814
* }
915
*/
10-
public class Solution {
11-
12-
int sum = 0;
16+
class Solution {
17+
private int ans;
1318

1419
public int findTilt(TreeNode root) {
15-
traverse(root);
16-
return sum;
20+
ans = 0;
21+
sum(root);
22+
return ans;
1723
}
1824

19-
public int traverse(TreeNode root) {
20-
if (root == null) return 0;
21-
int left = traverse(root.left);
22-
int right = traverse(root.right);
23-
sum += Math.abs(left - right);
24-
return left + right + root.val;
25+
private int sum(TreeNode root) {
26+
if (root == null) {
27+
return 0;
28+
}
29+
int left = sum(root.left);
30+
int right = sum(root.right);
31+
ans += Math.abs(left - right);
32+
return root.val + left + right;
2533
}
2634
}

0 commit comments

Comments
 (0)