Skip to content

Commit adb7ca7

Browse files
refactor 563
1 parent 81203ea commit adb7ca7

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed
Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* Created by fishercoder on 4/23/17.
5-
*/
6-
73
import com.fishercoder.common.classes.TreeNode;
84

9-
/**Binary Tree Tilt
5+
/**
6+
* 563. Binary Tree Tilt
107
*
118
* Given a binary tree, return the tilt of the whole tree.
12-
13-
The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree node values.
14-
Null node has tilt 0.
15-
The tilt of the whole tree is defined as the sum of all nodes' tilt.
9+
* The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values
10+
* and the sum of all right subtree node values.
11+
* Null node has tilt 0.
12+
* The tilt of the whole tree is defined as the sum of all nodes' tilt.
1613
1714
Example:
1815
Input:
@@ -32,31 +29,33 @@
3229
The sum of node values in any subtree won't exceed the range of 32-bit integer.
3330
All the tilt values won't exceed the range of 32-bit integer.*/
3431
public class _563 {
32+
public static class Solution1 {
3533

36-
int tilt = 0;
34+
int tilt = 0;
3735

38-
public int findTilt(TreeNode root) {
39-
findTiltDfs(root);
40-
return tilt;
41-
}
42-
43-
public int findTiltDfs(TreeNode root) {
44-
if (root == null) {
45-
return 0;
46-
}
47-
int leftTilt = 0;
48-
if (root.left != null) {
49-
leftTilt = findTiltDfs(root.left);
36+
public int findTilt(TreeNode root) {
37+
findTiltDfs(root);
38+
return tilt;
5039
}
51-
int rightTilt = 0;
52-
if (root.right != null) {
53-
rightTilt = findTiltDfs(root.right);
54-
}
55-
if (root.left == null && root.right == null) {
56-
return root.val;
40+
41+
public int findTiltDfs(TreeNode root) {
42+
if (root == null) {
43+
return 0;
44+
}
45+
int leftTilt = 0;
46+
if (root.left != null) {
47+
leftTilt = findTiltDfs(root.left);
48+
}
49+
int rightTilt = 0;
50+
if (root.right != null) {
51+
rightTilt = findTiltDfs(root.right);
52+
}
53+
if (root.left == null && root.right == null) {
54+
return root.val;
55+
}
56+
tilt += Math.abs(leftTilt - rightTilt);
57+
return leftTilt + rightTilt + root.val;
5758
}
58-
tilt += Math.abs(leftTilt - rightTilt);
59-
return leftTilt + rightTilt + root.val;
6059
}
6160

6261
}

src/test/java/com/fishercoder/_563Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
* Created by fishercoder on 4/23/17.
1313
*/
1414
public class _563Test {
15-
private static _563 test;
15+
private static _563.Solution1 solution1;
1616
private static int expected;
1717
private static int actual;
1818
private static TreeNode root;
1919

2020
@BeforeClass
2121
public static void setup() {
22-
test = new _563();
22+
solution1 = new _563.Solution1();
2323
actual = 0;
2424
}
2525

@@ -29,7 +29,7 @@ public void test1() {
2929
root.left = new TreeNode(2);
3030
root.right = new TreeNode(3);
3131
expected = 1;
32-
actual = test.findTilt(root);
32+
actual = solution1.findTilt(root);
3333
assertEquals(expected, actual);
3434
}
3535

@@ -42,7 +42,7 @@ public void test2() {
4242
root.left.left = new TreeNode(4);
4343
root.right.left = new TreeNode(5);
4444
expected = 11;
45-
actual = test.findTilt(root);
45+
actual = solution1.findTilt(root);
4646
assertEquals(expected, actual);
4747
}
4848

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy