Skip to content

Commit 3e9c5aa

Browse files
add java solution for leetcode 563
1 parent d45bd00 commit 3e9c5aa

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ LeetCode
7474
|623|[Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/) | |Medium|
7575
|581|[Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/) | |Easy|
7676
|572|[Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/) | |Easy|
77-
|563|[Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | |Easy|
77+
|563|[Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [java](./algorithms/binaryTreeTilt/Solution.java) |Easy|
7878
|547|[Friend Circles](https://leetcode.com/problems/friend-circles/) | |Medium|
7979
|543|[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/) | |Easy|
8080
|538|[Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/) | |Easy|
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Solution {
2+
int tilt = 0;
3+
4+
public int findTilt(TreeNode root) {
5+
if (null == root) {
6+
return 0;
7+
}
8+
iterationNodeTilt(root);
9+
return tilt;
10+
}
11+
12+
private int iterationNodeTilt(TreeNode root) {
13+
if (null == root) {
14+
return 0;
15+
}
16+
int leftTilt = iterationNodeTilt(root.left);
17+
int rightTilt = iterationNodeTilt(root.right);
18+
19+
tilt += Math.abs(leftTilt - rightTilt);
20+
return leftTilt + rightTilt + root.val;
21+
}
22+
}

0 commit comments

Comments
 (0)