forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_563.java
35 lines (29 loc) · 894 Bytes
/
_563.java
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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
public class _563 {
public static class Solution1 {
int tilt = 0;
public int findTilt(TreeNode root) {
findTiltDfs(root);
return tilt;
}
public int findTiltDfs(TreeNode root) {
if (root == null) {
return 0;
}
int leftTilt = 0;
if (root.left != null) {
leftTilt = findTiltDfs(root.left);
}
int rightTilt = 0;
if (root.right != null) {
rightTilt = findTiltDfs(root.right);
}
if (root.left == null && root.right == null) {
return root.val;
}
tilt += Math.abs(leftTilt - rightTilt);
return leftTilt + rightTilt + root.val;
}
}
}