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

Commit38bdb78

Browse files
refactor 144
1 parente787afd commit38bdb78

File tree

1 file changed

+42
-58
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+42
-58
lines changed

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

Lines changed: 42 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,71 +8,55 @@
88
importjava.util.List;
99
importjava.util.Stack;
1010

11-
/**
12-
* 144. Binary Tree Preorder Traversal
13-
14-
Given a binary tree, return the preorder traversal of its nodes' values.
15-
16-
For example:
17-
Given binary tree {1,#,2,3},
18-
1
19-
\
20-
2
21-
/
22-
3
23-
return [1,2,3].
24-
25-
Note: Recursive solution is trivial, could you do it iteratively?*/
26-
2711
publicclass_144 {
28-
publicstaticclassSolution1 {
29-
publicList<Integer>preorderTraversal(TreeNoderoot) {
30-
List<Integer>list =newArrayList<>();
31-
Stack<TreeNode>stack =newStack<>();
32-
stack.push(root);
33-
while (!stack.isEmpty()) {
34-
TreeNodecurr =stack.pop();
35-
if (curr !=null) {
36-
list.add(curr.val);
37-
stack.push(curr.right);
38-
stack.push(curr.left);
12+
publicstaticclassSolution1 {
13+
publicList<Integer>preorderTraversal(TreeNoderoot) {
14+
List<Integer>list =newArrayList<>();
15+
Stack<TreeNode>stack =newStack<>();
16+
stack.push(root);
17+
while (!stack.isEmpty()) {
18+
TreeNodecurr =stack.pop();
19+
if (curr !=null) {
20+
list.add(curr.val);
21+
stack.push(curr.right);
22+
stack.push(curr.left);
23+
}
24+
}
25+
returnlist;
3926
}
40-
}
41-
returnlist;
4227
}
43-
}
4428

45-
publicstaticclassSolution2 {
46-
publicList<Integer>preorderTraversal(TreeNoderoot) {
47-
List<Integer>list =newArrayList();
48-
returnpre(root,list);
49-
}
29+
publicstaticclassSolution2 {
30+
publicList<Integer>preorderTraversal(TreeNoderoot) {
31+
List<Integer>list =newArrayList();
32+
returnpre(root,list);
33+
}
5034

51-
List<Integer>pre(TreeNoderoot,List<Integer>list) {
52-
if (root ==null) {
53-
returnlist;
54-
}
55-
list.add(root.val);
56-
pre(root.left,list);
57-
pre(root.right,list);
58-
returnlist;
35+
List<Integer>pre(TreeNoderoot,List<Integer>list) {
36+
if (root ==null) {
37+
returnlist;
38+
}
39+
list.add(root.val);
40+
pre(root.left,list);
41+
pre(root.right,list);
42+
returnlist;
43+
}
5944
}
60-
}
6145

62-
publicstaticclassSolution3 {
63-
publicList<Integer>preorderTraversal(TreeNoderoot) {
64-
List<Integer>list =newArrayList<>();
65-
Stack<TreeNode>stack =newStack<>();
66-
while (!stack.isEmpty() ||root !=null) {
67-
while (root !=null) {
68-
list.add(root.val);
69-
stack.push(root);
70-
root =root.left;
46+
publicstaticclassSolution3 {
47+
publicList<Integer>preorderTraversal(TreeNoderoot) {
48+
List<Integer>list =newArrayList<>();
49+
Stack<TreeNode>stack =newStack<>();
50+
while (!stack.isEmpty() ||root !=null) {
51+
while (root !=null) {
52+
list.add(root.val);
53+
stack.push(root);
54+
root =root.left;
55+
}
56+
root =stack.pop();
57+
root =root.right;
58+
}
59+
returnlist;
7160
}
72-
root =stack.pop();
73-
root =root.right;
74-
}
75-
returnlist;
7661
}
77-
}
7862
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp