8000 refactor 545 · PriyatamRoy/Leetcode@7fb9c68 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7fb9c68

Browse files
refactor 545
1 parent 140027b commit 7fb9c68

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

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

Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
/**
99
* 545. Boundary of Binary Tree
10+
*
1011
* Given a binary tree, return the values of its boundary in anti-clockwise direction starting from root.
1112
* Boundary includes left boundary, addLeaves, and right boundary in order without duplicate nodes.
1213
* Left boundary is defined as the path from root to the left-most node.
@@ -58,56 +59,58 @@
5859
5960
*/
6061
public class _545 {
61-
public List<Integer> boundaryOfBinaryTree(TreeNode root) {
62-
List<Integer> nodes = new ArrayList<>();
63-
if (root == null) {
64-
return nodes;
65-
}
66-
67-
nodes.add(root.val);
68-
leftBoundary(root.left, nodes);
69-
addLeaves(root.left, nodes);
70-
addLeaves(root.right, nodes);
71-
rightBoundary(root.right, nodes);
72-
return nodes;
73-
}
62+
public static class Solution1 {
63+
public List<Integer> boundaryOfBinaryTree(TreeNode root) {
64+
List<Integer> nodes = new ArrayList<>();
65+
if (root == null) {
66+
return nodes;
67+
}
7468

75-
public void leftBoundary(TreeNode root, List<Integer> nodes) {
76-
if (root == null || (root.left == null && root.right == null)) {
77-
/**we don't want to add any LEAVES in leftBoundary and rightBoundary functions either,
78-
* that's why we have the later condition in the if branch.*/
79-
return;
80-
}
81-
nodes.add(root.val);// add BEFORE child visit
82-
if (root.left == null) {
83-
leftBoundary(root.right, nodes);
84-
} else {
69+
nodes.add(root.val);
8570
leftBoundary(root.left, nodes);
71+
addLeaves(root.left, nodes);
72+
addLeaves(root.right, nodes);
73+
rightBoundary(root.right, nodes);
74+
return nodes;
8675
}
87-
}
8876

89-
public void rightBoundary(TreeNode root, List<Integer> nodes) {
90-
if (root == null || (root.right == null && root.left == null)) {
91-
return;
92-
}
93-
if (root.right == null) {
94-
rightBoundary(root.left, nodes);
95-
} else {
96-
rightBoundary(root.right, nodes);
77+
public void leftBoundary(TreeNode root, List<Integer> nodes) {
78+
if (root == null || (root.left == null && root.right == null)) {
79+
/**we don't want to add any LEAVES in leftBoundary and rightBoundary functions either,
80+
* that's why we have the later condition in the if branch.*/
81+
return;
82+
}
83+
nodes.add(root.val);// add BEFORE child visit
84+
if (root.left == null) {
85+
leftBoundary(root.right, nodes);
86+
} else {
87+
leftBoundary(root.left, nodes);
88+
}
9789
}
98-
nodes.add(root.val); // add AFTER child visit(reverse)
99-
}
10090

101-
public void addLeaves(TreeNode root, List<Integer> nodes) {
102-
if (root == null) {
103-
return;
91+
public void rightBoundary(TreeNode root, List<Integer> nodes) {
92+
if (root == null || (root.right == null && root.left == null)) {
93+
return;
94+
}
95+
if (root.right == null) {
96+
rightBoundary(root.left, nodes);
97+
} else {
98+
rightBoundary(root.right, nodes);
99+
}
100+
nodes.add(root.val); // add AFTER child visit(reverse)
104101
}
105-
if (root.left == null && root.right == null) {
106-
nodes.add(root.val);
107-
return;
102+
103+
public void addLeaves(TreeNode root, List<Integer> nodes) {
104+
if (root == null) {
105+
return;
106+
}
107+
if (root.left == null && root.right == null) {
108+
nodes.add(root.val);
109+
return;
110+
}
111+
addLeaves(root.left, nodes);
112+
addLeaves(root.right, nodes);
108113
}
109-
addLeaves(root.left, nodes);
110-
addLeaves(root.right, nodes);
111114
}
112115

113116
}

src/test/java/com/fishercoder/_545Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
import static junit.framework.Assert.assertEquals;
1313

1414
public class _545Test {
15-
private static _545 test;
15+
private static _545.Solution1 test;
1616
private static TreeNode root;
1717
private static List<Integer> expected;
1818

1919
@BeforeClass
2020
public static void setup() {
21-
test = new _545();
21+
test = new _545.Solution1();
2222
}
2323

2424
@Test

0 commit comments

Comments
 (0)
0