Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita060f63

Browse files
[LEET-545] add 545
1 parent4c5703f commita060f63

File tree

3 files changed

+248
-0
lines changed

3 files changed

+248
-0
lines changed

‎leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
##Algorithms
44
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
55
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
6+
|545|[Boundary of Binary Tree](https://leetcode.com/problems/boundary-of-binary-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BoundaryofBinaryTree.java) | O(n) |O(n) | Medium | Recursion
67
|544|[Output Contest Matches](https://leetcode.com/problems/output-contest-matches/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/OutputContestMatches.java) | O(n) |O(n) | Medium | Recursion
78
|543|[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/DiameterofBinaryTree.java) | O(n) |O(h) | Easy | Tree/DFS/Recursion
89
|542|[01 Matrix](https://leetcode.com/problems/01-matrix/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/_01Matrix.java) | O(m*n) |O(n) | Medium | BFS
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
packagecom.stevesun.solutions;
2+
3+
importcom.stevesun.common.classes.TreeNode;
4+
5+
importjava.util.ArrayList;
6+
importjava.util.HashSet;
7+
importjava.util.List;
8+
importjava.util.Set;
9+
10+
/**
11+
* Given a binary tree, return the values of its boundary in anti-clockwise direction starting from root. Boundary includes left boundary, leaves, and right boundary in order without duplicate nodes.
12+
13+
Left boundary is defined as the path from root to the left-most node. Right boundary is defined as the path from root to the right-most node. If the root doesn't have left subtree or right subtree, then the root itself is left boundary or right boundary. Note this definition only applies to the input binary tree, and not applies to any subtrees.
14+
15+
The left-most node is defined as a leaf node you could reach when you always firstly travel to the left subtree if exists. If not, travel to the right subtree. Repeat until you reach a leaf node.
16+
17+
The right-most node is also defined by the same way with left and right exchanged.
18+
19+
Example 1
20+
Input:
21+
1
22+
\
23+
2
24+
/ \
25+
3 4
26+
27+
Ouput:
28+
[1, 3, 4, 2]
29+
30+
Explanation:
31+
The root doesn't have left subtree, so the root itself is left boundary.
32+
The leaves are node 3 and 4.
33+
The right boundary are node 1,2,4. Note the anti-clockwise direction means you should output reversed right boundary.
34+
So order them in anti-clockwise without duplicates and we have [1,3,4,2].
35+
36+
37+
Example 2
38+
Input:
39+
____1_____
40+
/ \
41+
2 3
42+
/ \ /
43+
4 5 6
44+
/ \ / \
45+
7 8 9 10
46+
47+
Ouput:
48+
[1,2,4,7,8,9,10,6,3]
49+
50+
Explanation:
51+
The left boundary are node 1,2,4. (4 is the left-most node according to definition)
52+
The leaves are node 4,7,8,9,10.
53+
The right boundary are node 1,3,6,10. (10 is the right-most node).
54+
So order them in anti-clockwise without duplicate nodes we have [1,2,4,7,8,9,10,6,3].
55+
56+
*/
57+
publicclassBoundaryofBinaryTree {
58+
publicList<Integer>boundaryOfBinaryTree(TreeNoderoot) {
59+
List<Integer>nodes =newArrayList<>(1000);
60+
if(root ==null)returnnodes;
61+
62+
nodes.add(root.val);
63+
leftBoundary(root.left,nodes);
64+
leaves(root.left,nodes);
65+
leaves(root.right,nodes);
66+
rightBoundary(root.right,nodes);
67+
returnnodes;
68+
}
69+
publicvoidleftBoundary(TreeNoderoot,List<Integer>nodes) {
70+
if(root ==null || (root.left ==null &&root.right ==null))return;
71+
nodes.add(root.val);
72+
if(root.left ==null)leftBoundary(root.right,nodes);
73+
elseleftBoundary(root.left,nodes);
74+
}
75+
publicvoidrightBoundary(TreeNoderoot,List<Integer>nodes) {
76+
if(root ==null || (root.right ==null &&root.left ==null))return;
77+
if(root.right ==null)rightBoundary(root.left,nodes);
78+
elserightBoundary(root.right,nodes);
79+
nodes.add(root.val);// add after child visit(reverse)
80+
}
81+
publicvoidleaves(TreeNoderoot,List<Integer>nodes) {
82+
if(root ==null)return;
83+
if(root.left ==null &&root.right ==null) {
84+
nodes.add(root.val);
85+
return;
86+
}
87+
leaves(root.left,nodes);
88+
leaves(root.right,nodes);
89+
}
90+
91+
92+
//assume the tree has no duplicate values
93+
publicList<Integer>boundaryOfBinaryTree_failed_attempt(TreeNoderoot) {
94+
//this my original naive attemp failed by 79/117 test cases, specifically by {@link BoundaryofBinaryTreeTest.test5}.
95+
List<Integer>leftBoundary =newArrayList<>();
96+
leftBoundary =findLeftBoundary(root,leftBoundary);
97+
List<Integer>leaves =newArrayList<>();
98+
leaves =findLeaves(root,leaves);
99+
List<Integer>rightBoundary =newArrayList<>();
100+
rightBoundary =findRightBoundary(root,rightBoundary);
101+
List<Integer>boundary =newArrayList<>();
102+
Set<Integer>set =newHashSet<>();
103+
for (inti =0;i <leftBoundary.size();i++) {
104+
boundary.add(leftBoundary.get(i));
105+
set.add(leftBoundary.get(i));
106+
}
107+
for (inti =0;i <leaves.size();i++) {
108+
if (!set.add(leaves.get(i)))continue;
109+
boundary.add(leaves.get(i));
110+
}
111+
for (inti =rightBoundary.size()-1;i >=0;i--) {
112+
if (!set.add(rightBoundary.get(i)))continue;
113+
boundary.add(rightBoundary.get(i));
114+
}
115+
returnboundary;
116+
}
117+
118+
privateList<Integer>findRightBoundary(TreeNoderoot,List<Integer>rightBoundary) {
119+
if (root ==null)returnrightBoundary;
120+
if (root.right !=null) {
121+
rightBoundary.add(root.right.val);
122+
findRightBoundary(root.right,rightBoundary);
123+
}elseif (root.left !=null) {
124+
rightBoundary.add(root.left.val);
125+
findRightBoundary(root.left,rightBoundary);
126+
}elseif (root.left ==null &&root.right ==null) {
127+
returnrightBoundary;
128+
}
129+
returnrightBoundary;
130+
}
131+
132+
privateList<Integer>findLeaves(TreeNoderoot,List<Integer>leaves) {
133+
if (root ==null)returnleaves;
134+
if (root.left !=null) {
135+
findLeaves(root.left,leaves);
136+
}
137+
if (root.right !=null) {
138+
findLeaves(root.right,leaves);
139+
}
140+
if (root.left ==null &&root.right ==null) {
141+
leaves.add(root.val);
142+
}
143+
returnleaves;
144+
}
145+
146+
privateList<Integer>findLeftBoundary(TreeNoderoot,List<Integer>leftBoundary) {
147+
if (root ==null) {
148+
returnleftBoundary;
149+
}
150+
if (root !=null) {
151+
leftBoundary.add(root.val);
152+
findLeftBoundary(root.left,leftBoundary);
153+
}
154+
returnleftBoundary;
155+
}
156+
157+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
packagecom.stevesun;
2+
3+
importcom.stevesun.common.classes.TreeNode;
4+
importcom.stevesun.solutions.BoundaryofBinaryTree;
5+
importorg.junit.Before;
6+
importorg.junit.BeforeClass;
7+
importorg.junit.Test;
8+
9+
importjava.util.ArrayList;
10+
importjava.util.Arrays;
11+
importjava.util.List;
12+
13+
importstaticjunit.framework.Assert.assertEquals;
14+
15+
publicclassBoundaryofBinaryTreeTest {
16+
privatestaticBoundaryofBinaryTreetest;
17+
privatestaticList<Integer>expected;
18+
privatestaticList<Integer>actual;
19+
privatestaticTreeNoderoot;
20+
21+
@BeforeClass
22+
publicstaticvoidsetup(){
23+
test =newBoundaryofBinaryTree();
24+
}
25+
26+
@Before
27+
publicvoidsetupForEachTest(){}
28+
29+
@Test
30+
publicvoidtest1(){
31+
root =newTreeNode(1);
32+
root.right =newTreeNode(2);
33+
root.right.left =newTreeNode(3);
34+
root.right.right =newTreeNode(4);
35+
actual =test.boundaryOfBinaryTree(root);
36+
expected =newArrayList<>(Arrays.asList(1,3,4 ,2));
37+
assertEquals(expected,actual);
38+
}
39+
40+
@Test
41+
publicvoidtest2(){
42+
root =newTreeNode(1);
43+
root.left =newTreeNode(2);
44+
root.left.left =newTreeNode(4);
45+
root.left.right =newTreeNode(5);
46+
root.left.right.left =newTreeNode(7);
47+
root.left.right.right =newTreeNode(8);
48+
root.right =newTreeNode(3);
49+
root.right.left =newTreeNode(6);
50+
root.right.left.left =newTreeNode(9);
51+
root.right.left.right =newTreeNode(10);
52+
actual =test.boundaryOfBinaryTree(root);
53+
expected =newArrayList<>(Arrays.asList(1,2,4,7,8,9,10,6,3));
54+
assertEquals(expected,actual);
55+
56+
}
57+
58+
@Test
59+
publicvoidtest3(){
60+
root =newTreeNode(1);
61+
actual =test.boundaryOfBinaryTree(root);
62+
expected =newArrayList<>(Arrays.asList(1));
63+
assertEquals(expected,actual);
64+
}
65+
66+
@Test
67+
publicvoidtest4(){
68+
root =newTreeNode(1);
69+
root.left =newTreeNode(2);
70+
root.left.left =newTreeNode(3);
71+
root.left.left.left=newTreeNode(4);
72+
actual =test.boundaryOfBinaryTree(root);
73+
expected =newArrayList<>(Arrays.asList(1,2,3,4));
74+
assertEquals(expected,actual);
75+
}
76+
77+
@Test
78+
publicvoidtest5(){
79+
root =newTreeNode(1);
80+
root.left =newTreeNode(2);
81+
root.left.right =newTreeNode(4);
82+
root.right =newTreeNode(3);
83+
root.right.left =newTreeNode(5);
84+
root.left.right.left =newTreeNode(6);
85+
root.left.right.right =newTreeNode(7);
86+
actual =test.boundaryOfBinaryTree(root);
87+
expected =newArrayList<>(Arrays.asList(1,2,4,6,7,5,3));
88+
assertEquals(expected,actual);
89+
}
90+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp