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

Commitd8d6604

Browse files
refactor 226
1 parent6c4c188 commitd8d6604

File tree

1 file changed

+49
-73
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+49
-73
lines changed

‎src/main/java/com/fishercoder/solutions/_226.java

Lines changed: 49 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,79 +5,55 @@
55
importjava.util.LinkedList;
66
importjava.util.Queue;
77

8-
/**
9-
* 226. Invert Binary Tree
10-
11-
Invert a binary tree.
12-
13-
4
14-
/ \
15-
2 7
16-
/ \ / \
17-
1 3 6 9
18-
19-
to
20-
21-
4
22-
/ \
23-
7 2
24-
/ \ / \
25-
9 6 3 1
26-
27-
Trivia:
28-
This problem was inspired by this original tweet by Max Howell:
29-
Google: 90% of our engineers use the software you wrote (Homebrew),
30-
but you can�t invert a binary tree on a whiteboard so fuck off.
31-
*/
328
publicclass_226 {
339

34-
publicstaticclassSolution1 {
35-
publicTreeNodeinvertTree(TreeNoderoot) {
36-
if (root ==null) {
37-
returnroot;
38-
}
39-
Queue<TreeNode>q =newLinkedList();
40-
q.offer(root);
41-
while (!q.isEmpty()) {
42-
TreeNodecurr =q.poll();
43-
TreeNodetemp =curr.left;
44-
curr.left =curr.right;
45-
curr.right =temp;
46-
if (curr.left !=null) {
47-
q.offer(curr.left);
48-
}
49-
if (curr.right !=null) {
50-
q.offer(curr.right);
51-
}
52-
}
53-
returnroot;
54-
}
55-
}
56-
57-
publicstaticclassSolution2 {
58-
publicTreeNodeinvertTree(TreeNoderoot) {
59-
if (root ==null) {
60-
returnroot;
61-
}
62-
TreeNodetemp =root.left;
63-
root.left =root.right;
64-
root.right =temp;
65-
invertTree(root.left);
66-
invertTree(root.right);
67-
returnroot;
68-
}
69-
}
70-
71-
publicstaticclassSolution3 {
72-
//more concise version
73-
publicTreeNodeinvertTree(TreeNoderoot) {
74-
if (root ==null) {
75-
returnroot;
76-
}
77-
TreeNodetemp =root.left;
78-
root.left =invertTree(root.right);
79-
root.right =invertTree(temp);
80-
returnroot;
81-
}
82-
}
10+
publicstaticclassSolution1 {
11+
publicTreeNodeinvertTree(TreeNoderoot) {
12+
if (root ==null) {
13+
returnroot;
14+
}
15+
Queue<TreeNode>q =newLinkedList();
16+
q.offer(root);
17+
while (!q.isEmpty()) {
18+
TreeNodecurr =q.poll();
19+
TreeNodetemp =curr.left;
20+
curr.left =curr.right;
21+
curr.right =temp;
22+
if (curr.left !=null) {
23+
q.offer(curr.left);
24+
}
25+
if (curr.right !=null) {
26+
q.offer(curr.right);
27+
}
28+
}
29+
returnroot;
30+
}
31+
}
32+
33+
publicstaticclassSolution2 {
34+
publicTreeNodeinvertTree(TreeNoderoot) {
35+
if (root ==null) {
36+
returnroot;
37+
}
38+
TreeNodetemp =root.left;
39+
root.left =root.right;
40+
root.right =temp;
41+
invertTree(root.left);
42+
invertTree(root.right);
43+
returnroot;
44+
}
45+
}
46+
47+
publicstaticclassSolution3 {
48+
//more concise version
49+
publicTreeNodeinvertTree(TreeNoderoot) {
50+
if (root ==null) {
51+
returnroot;
52+
}
53+
TreeNodetemp =root.left;
54+
root.left =invertTree(root.right);
55+
root.right =invertTree(temp);
56+
returnroot;
57+
}
58+
}
8359
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp