Skip to content

Commit 727f2aa

Browse files
[N-0] refactor 543
1 parent 95a1d4c commit 727f2aa

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

src/main/java/com/fishercoder/solutions/_543.java

+30-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
import com.fishercoder.common.classes.TreeNode;
44

55
/**
6-
* Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
6+
* 543. Diameter of Binary Tree
7+
*
8+
* Given a binary tree, you need to compute the length of the diameter of the tree.
9+
* The diameter of a binary tree is the length of the longest path between any two nodes in a tree.
10+
* This path may or may not pass through the root.
711
812
Example:
913
Given a binary tree
@@ -18,21 +22,32 @@
1822
*/
1923
public class _543 {
2024

21-
int diameter = 0;
22-
23-
public int diameterOfBinaryTree(TreeNode root) {
24-
dfs(root);
25-
return diameter;
26-
}
25+
public static class Solution1 {
26+
/**This is a very great problem for practicing recursion:
27+
* 1. What dfs() returns is the max height it should pick from either its left or right subtree, that's
28+
* what the int return type stands for;
29+
* 2. And during the recursion, we can keep updating the global variable: "diameter";
30+
* 3. When computing length/height of a subtree, we should take the max of its left and right, then plus one
31+
* and left height should be like this
32+
* int left = dfs(root.left);
33+
* instead of dfs(root.left) + 1;
34+
* we'll only plus one at the end
35+
* */
36+
int diameter = 0;
37+
38+
public int diameterOfBinaryTree(TreeNode root) {
39+
dfs(root);
40+
return diameter;
41+
}
2742

28-
private int dfs(TreeNode root) {
29-
if (root == null) {
30-
return 0;
43+
private int dfs(TreeNode root) {
44+
if (root == null) {
45+
return 0;
46+
}
47+
int left = dfs(root.left);
48+
int right = dfs(root.right);
49+
diameter = Math.max(diameter, left + right);
50+
return Math.max(left, right) + 1;
3151
}
32-
int left = dfs(root.left);
33-
int right = dfs(root.right);
34-
diameter = Math.max(diameter, left + right);
35-
return Math.max(left, right) + 1;
3652
}
37-
3853
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._543;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static junit.framework.TestCase.assertEquals;
12+
13+
public class _543Test {
14+
private static _543.Solution1 solution1;
15+
private static TreeNode root;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _543.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5));
25+
TreeUtils.printBinaryTree(root);
26+
assertEquals(3, solution1.diameterOfBinaryTree(root));
27+
}
28+
29+
}

0 commit comments

Comments
 (0)