File tree 2 files changed +23
-1
lines changed
algorithms/binaryTreeTilt
2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -74,7 +74,7 @@ LeetCode
74
74
| 623| [ Add One Row to Tree] ( https://leetcode.com/problems/add-one-row-to-tree/ ) | | Medium|
75
75
| 581| [ Shortest Unsorted Continuous Subarray] ( https://leetcode.com/problems/shortest-unsorted-continuous-subarray/ ) | | Easy|
76
76
| 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|
78
78
| 547| [ Friend Circles] ( https://leetcode.com/problems/friend-circles/ ) | | Medium|
79
79
| 543| [ Diameter of Binary Tree] ( https://leetcode.com/problems/diameter-of-binary-tree/ ) | | Easy|
80
80
| 538| [ Convert BST to Greater Tree] ( https://leetcode.com/problems/convert-bst-to-greater-tree/ ) | | Easy|
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments