Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wwwanlingxiao
LeetCodeAnimation
Commits
921cc799
Commit
921cc799
authored
Dec 07, 2018
by
MisterBigbooo
Browse files
添加仓库代码
parent
4e5b4643
Changes
137
Hide whitespace changes
Inline
Side-by-side
0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution3.java
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
// Classic Non-Recursive algorithm for preorder traversal
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
public
class
Solution3
{
public
List
<
Integer
>
preorderTraversal
(
TreeNode
root
)
{
ArrayList
<
Integer
>
res
=
new
ArrayList
<
Integer
>();
if
(
root
==
null
)
return
res
;
Stack
<
TreeNode
>
stack
=
new
Stack
<
TreeNode
>();
stack
.
push
(
root
);
while
(!
stack
.
empty
()){
TreeNode
curNode
=
stack
.
pop
();
res
.
add
(
curNode
.
val
);
if
(
curNode
.
right
!=
null
)
stack
.
push
(
curNode
.
right
);
if
(
curNode
.
left
!=
null
)
stack
.
push
(
curNode
.
left
);
}
return
res
;
}
}
0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution4.java
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-30
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
// Another Classic Non-Recursive algorithm for preorder traversal
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
public
class
Solution4
{
public
List
<
Integer
>
preorderTraversal
(
TreeNode
root
)
{
ArrayList
<
Integer
>
res
=
new
ArrayList
<
Integer
>();
if
(
root
==
null
)
return
res
;
Stack
<
TreeNode
>
stack
=
new
Stack
<
TreeNode
>();
TreeNode
cur
=
root
;
while
(
cur
!=
null
||
!
stack
.
isEmpty
()){
while
(
cur
!=
null
){
res
.
add
(
cur
.
val
);
stack
.
push
(
cur
);
cur
=
cur
.
left
;
}
cur
=
stack
.
pop
();
cur
=
cur
.
right
;
}
return
res
;
}
}
0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution5.java
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-30
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
// Another Classic Non-Recursive algorithm for preorder traversal
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
public
class
Solution5
{
public
List
<
Integer
>
preorderTraversal
(
TreeNode
root
)
{
ArrayList
<
Integer
>
res
=
new
ArrayList
<
Integer
>();
if
(
root
==
null
)
return
res
;
Stack
<
TreeNode
>
stack
=
new
Stack
<
TreeNode
>();
TreeNode
cur
=
root
;
while
(
cur
!=
null
||
!
stack
.
isEmpty
()){
if
(
cur
!=
null
){
res
.
add
(
cur
.
val
);
stack
.
push
(
cur
);
cur
=
cur
.
left
;
}
else
{
cur
=
stack
.
pop
();
cur
=
cur
.
right
;
}
}
return
res
;
}
}
0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution6.java
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-29
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
// PreOrder Morris Traversal
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(1)
public
class
Solution6
{
public
List
<
Integer
>
preorderTraversal
(
TreeNode
root
)
{
ArrayList
<
Integer
>
res
=
new
ArrayList
<
Integer
>();
if
(
root
==
null
)
return
res
;
TreeNode
cur
=
root
;
while
(
cur
!=
null
){
if
(
cur
.
left
==
null
){
res
.
add
(
cur
.
val
);
cur
=
cur
.
right
;
}
else
{
TreeNode
prev
=
cur
.
left
;
while
(
prev
.
right
!=
null
&&
prev
.
right
!=
cur
)
prev
=
prev
.
right
;
if
(
prev
.
right
==
null
){
res
.
add
(
cur
.
val
);
prev
.
right
=
cur
;
cur
=
cur
.
left
;
}
else
{
prev
.
right
=
null
;
cur
=
cur
.
right
;
}
}
}
return
res
;
}
}
0144-Binary-Tree-Preorder-Traversal/java-0144/src/TreeNode.java
0 → 100755
View file @
921cc799
// Definition for a binary tree node.
public
class
TreeNode
{
int
val
;
TreeNode
left
;
TreeNode
right
;
TreeNode
(
int
x
)
{
val
=
x
;
}
}
\ No newline at end of file
0145-Binary-Tree-Postorder-Traversal/cpp-0145/CMakeLists.txt
0 → 100755
View file @
921cc799
cmake_minimum_required
(
VERSION 3.5
)
project
(
cpp_0145
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-std=c++11"
)
set
(
SOURCE_FILES main8.cpp
)
add_executable
(
cpp_0145
${
SOURCE_FILES
}
)
\ No newline at end of file
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main.cpp
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
#include
<iostream>
#include
<vector>
using
namespace
std
;
/// Definition for a binary tree node.
struct
TreeNode
{
int
val
;
TreeNode
*
left
;
TreeNode
*
right
;
TreeNode
(
int
x
)
:
val
(
x
),
left
(
NULL
),
right
(
NULL
)
{}
};
// Recursive
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
class
Solution
{
public:
vector
<
int
>
postorderTraversal
(
TreeNode
*
root
)
{
vector
<
int
>
res
;
__postorderTraversal
(
root
,
res
);
return
res
;
}
private:
void
__postorderTraversal
(
TreeNode
*
node
,
vector
<
int
>
&
res
){
if
(
node
){
__postorderTraversal
(
node
->
left
,
res
);
__postorderTraversal
(
node
->
right
,
res
);
res
.
push_back
(
node
->
val
);
}
}
};
int
main
()
{
return
0
;
}
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main2.cpp
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
#include
<iostream>
#include
<vector>
using
namespace
std
;
/// Definition for a binary tree node.
struct
TreeNode
{
int
val
;
TreeNode
*
left
;
TreeNode
*
right
;
TreeNode
(
int
x
)
:
val
(
x
),
left
(
NULL
),
right
(
NULL
)
{}
};
// My Non-Recursive
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
class
Solution
{
private:
struct
Command
{
string
s
;
// go, print
TreeNode
*
node
;
Command
(
string
s
,
TreeNode
*
node
)
:
s
(
s
),
node
(
node
){}
};
public:
vector
<
int
>
postorderTraversal
(
TreeNode
*
root
)
{
vector
<
int
>
res
;
if
(
root
==
NULL
)
return
res
;
stack
<
Command
>
stack
;
stack
.
push
(
Command
(
"go"
,
root
)
);
while
(
!
stack
.
empty
()){
Command
command
=
stack
.
top
();
stack
.
pop
();
if
(
command
.
s
==
"print"
)
res
.
push_back
(
command
.
node
->
val
);
else
{
assert
(
command
.
s
==
"go"
);
stack
.
push
(
Command
(
"print"
,
command
.
node
));
if
(
command
.
node
->
right
)
stack
.
push
(
Command
(
"go"
,
command
.
node
->
right
));
if
(
command
.
node
->
left
)
stack
.
push
(
Command
(
"go"
,
command
.
node
->
left
));
}
}
return
res
;
}
};
int
main
()
{
return
0
;
}
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main3.cpp
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-30
#include
<iostream>
#include
<vector>
#include
<stack>
using
namespace
std
;
/// Definition for a binary tree node.
struct
TreeNode
{
int
val
;
TreeNode
*
left
;
TreeNode
*
right
;
TreeNode
(
int
x
)
:
val
(
x
),
left
(
NULL
),
right
(
NULL
)
{}
};
// Non-Recursive
// Using a tag to record whether the node has been visited
//
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
class
Solution
{
private:
struct
TagNode
{
TreeNode
*
node
;
bool
isFirst
;
TagNode
(
TreeNode
*
node
)
:
node
(
node
),
isFirst
(
false
){}
};
public:
vector
<
int
>
postorderTraversal
(
TreeNode
*
root
)
{
vector
<
int
>
res
;
if
(
root
==
NULL
)
return
res
;
stack
<
TagNode
>
stack
;
TreeNode
*
cur
=
root
;
while
(
cur
!=
NULL
||
!
stack
.
empty
()){
while
(
cur
!=
NULL
){
stack
.
push
(
TagNode
(
cur
));
cur
=
cur
->
left
;
}
TagNode
tagNode
=
stack
.
top
();
stack
.
pop
();
cur
=
tagNode
.
node
;
if
(
tagNode
.
isFirst
==
false
){
tagNode
.
isFirst
=
true
;
stack
.
push
(
tagNode
);
cur
=
cur
->
right
;
}
else
{
res
.
push_back
(
cur
->
val
);
cur
=
NULL
;
};
}
return
res
;
}
};
int
main
()
{
return
0
;
}
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main4.cpp
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-30
#include
<iostream>
#include
<vector>
#include
<stack>
using
namespace
std
;
/// Definition for a binary tree node.
struct
TreeNode
{
int
val
;
TreeNode
*
left
;
TreeNode
*
right
;
TreeNode
(
int
x
)
:
val
(
x
),
left
(
NULL
),
right
(
NULL
)
{}
};
// Non-Recursive
// Using two stacks, Reverse the Preorder Traversal!
//
// Time Complexity: O(n)
// Space Complexity: O(n)
class
Solution
{
public:
vector
<
int
>
postorderTraversal
(
TreeNode
*
root
)
{
vector
<
int
>
res
;
if
(
root
==
NULL
)
return
res
;
stack
<
TreeNode
*>
stack
,
output
;
stack
.
push
(
root
);
while
(
!
stack
.
empty
()){
TreeNode
*
node
=
stack
.
top
();
stack
.
pop
();
output
.
push
(
node
);
if
(
node
->
left
!=
NULL
)
stack
.
push
(
node
->
left
);
if
(
node
->
right
!=
NULL
)
stack
.
push
(
node
->
right
);
}
while
(
!
output
.
empty
()){
res
.
push_back
((
output
.
top
())
->
val
);
output
.
pop
();
}
return
res
;
}
};
int
main
()
{
return
0
;
}
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main5.cpp
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-30
#include
<iostream>
#include
<vector>
#include
<stack>
using
namespace
std
;
/// Definition for a binary tree node.
struct
TreeNode
{
int
val
;
TreeNode
*
left
;
TreeNode
*
right
;
TreeNode
(
int
x
)
:
val
(
x
),
left
(
NULL
),
right
(
NULL
)
{}
};
// Non-Recursive
// Using two stacks, Reverse the Preorder Traversal!
//
// Time Complexity: O(n)
// Space Complexity: O(n)
class
Solution
{
public:
vector
<
int
>
postorderTraversal
(
TreeNode
*
root
)
{
vector
<
int
>
res
;
if
(
root
==
NULL
)
return
res
;
stack
<
TreeNode
*>
stack
,
output
;
TreeNode
*
p
=
root
;
while
(
p
!=
NULL
||
!
stack
.
empty
()){
if
(
p
!=
NULL
){
stack
.
push
(
p
);
output
.
push
(
p
);
p
=
p
->
right
;
}
else
{
p
=
stack
.
top
();
stack
.
pop
();
p
=
p
->
left
;
}
}
while
(
!
output
.
empty
()){
res
.
push_back
((
output
.
top
())
->
val
);
output
.
pop
();
}
return
res
;
}
};
int
main
()
{
return
0
;
}
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main6.cpp
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-31
#include
<iostream>
#include
<vector>
#include
<stack>
using
namespace
std
;
/// Definition for a binary tree node.
struct
TreeNode
{
int
val
;
TreeNode
*
left
;
TreeNode
*
right
;
TreeNode
(
int
x
)
:
val
(
x
),
left
(
NULL
),
right
(
NULL
)
{}
};
// Non-Recursive
// Using a pre pointer to record the last visted node
//
// Time Complexity: O(n)
// Space Complexity: O(h)
class
Solution
{
public:
vector
<
int
>
postorderTraversal
(
TreeNode
*
root
)
{
vector
<
int
>
res
;
if
(
root
==
NULL
)
return
res
;
stack
<
TreeNode
*>
stack
;
TreeNode
*
pre
=
NULL
;
stack
.
push
(
root
);
while
(
!
stack
.
empty
()){
TreeNode
*
node
=
stack
.
top
();
stack
.
pop
();
if
((
node
->
left
==
NULL
&&
node
->
right
==
NULL
)
||
(
pre
!=
NULL
&&
pre
==
node
->
left
&&
node
->
right
==
NULL
)
||
(
pre
!=
NULL
&&
pre
==
node
->
right
)){
res
.
push_back
(
node
->
val
);
pre
=
node
;
}
else
{
stack
.
push
(
node
);
if
(
node
->
right
!=
NULL
)
stack
.
push
(
node
->
right
);
if
(
node
->
left
!=
NULL
)
stack
.
push
(
node
->
left
);
}
}
return
res
;
}
};
void
print_vec
(
const
vector
<
int
>&
vec
){
for
(
int
e
:
vec
)
cout
<<
e
<<
" "
;
cout
<<
endl
;
}
int
main
()
{
TreeNode
*
root
=
new
TreeNode
(
1
);
root
->
right
=
new
TreeNode
(
2
);
root
->
right
->
left
=
new
TreeNode
(
3
);
print_vec
(
Solution
().
postorderTraversal
(
root
));
return
0
;
}
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main7.cpp
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-31
#include
<iostream>
#include
<vector>
#include
<stack>
using
namespace
std
;
/// Definition for a binary tree node.
struct
TreeNode
{
int
val
;
TreeNode
*
left
;
TreeNode
*
right
;
TreeNode
(
int
x
)
:
val
(
x
),
left
(
NULL
),
right
(
NULL
)
{}
};
// Classic Non-Recursive
// Using a pre pointer to record the last visted node
//
// Time Complexity: O(n)
// Space Complexity: O(h)
class
Solution
{
public:
vector
<
int
>
postorderTraversal
(
TreeNode
*
root
)
{
vector
<
int
>
res
;
if
(
root
==
NULL
)
return
res
;
stack
<
TreeNode
*>
stack
;
TreeNode
*
pre
=
NULL
;
TreeNode
*
cur
=
root
;
while
(
cur
!=
NULL
||
!
stack
.
empty
()){
while
(
cur
!=
NULL
){
stack
.
push
(
cur
);
cur
=
cur
->
left
;
}
cur
=
stack
.
top
();
stack
.
pop
();
if
(
cur
->
right
==
NULL
||
pre
==
cur
->
right
){
res
.
push_back
(
cur
->
val
);
pre
=
cur
;
cur
=
NULL
;
}
else
{
stack
.
push
(
cur
);
cur
=
cur
->
right
;
}
}
return
res
;
}
};
void
print_vec
(
const
vector
<
int
>&
vec
){
for
(
int
e
:
vec
)
cout
<<
e
<<
" "
;
cout
<<
endl
;
}
int
main
()
{
TreeNode
*
root
=
new
TreeNode
(
1
);
root
->
right
=
new
TreeNode
(
2
);
root
->
right
->
left
=
new
TreeNode
(
3
);
print_vec
(
Solution
().
postorderTraversal
(
root
));
return
0
;
}
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main8.cpp
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-31
#include
<iostream>
#include
<vector>
#include
<stack>
using
namespace
std
;
/// Definition for a binary tree node.
struct
TreeNode
{
int
val
;
TreeNode
*
left
;
TreeNode
*
right
;
TreeNode
(
int
x
)
:
val
(
x
),
left
(
NULL
),
right
(
NULL
)
{}
};
// Classic Non-Recursive
// Using a pre pointer to record the last visted node
//
// Time Complexity: O(n)
// Space Complexity: O(h)
class
Solution
{
public:
vector
<
int
>
postorderTraversal
(
TreeNode
*
root
)
{
vector
<
int
>
res
;
if
(
root
==
NULL
)
return
res
;
stack
<
TreeNode
*>
stack
;
TreeNode
*
pre
=
NULL
;
TreeNode
*
cur
=
root
;
while
(
cur
!=
NULL
||
!
stack
.
empty
()){
if
(
cur
!=
NULL
){
stack
.
push
(
cur
);
cur
=
cur
->
left
;
}
else
{
cur
=
stack
.
top
();
stack
.
pop
();
if
(
cur
->
right
==
NULL
||
pre
==
cur
->
right
){
res
.
push_back
(
cur
->
val
);
pre
=
cur
;
cur
=
NULL
;
}
else
{
stack
.
push
(
cur
);
cur
=
cur
->
right
;
}
}
}
return
res
;
}
};
void
print_vec
(
const
vector
<
int
>&
vec
){
for
(
int
e
:
vec
)
cout
<<
e
<<
" "
;
cout
<<
endl
;
}
int
main
()
{
TreeNode
*
root
=
new
TreeNode
(
1
);
root
->
right
=
new
TreeNode
(
2
);
root
->
right
->
left
=
new
TreeNode
(
3
);
print_vec
(
Solution
().
postorderTraversal
(
root
));
return
0
;
}
0145-Binary-Tree-Postorder-Traversal/cpp-0145/main9.cpp
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-31
#include
<iostream>
#include
<vector>
#include
<stack>
using
namespace
std
;
/// Definition for a binary tree node.
struct
TreeNode
{
int
val
;
TreeNode
*
left
;
TreeNode
*
right
;
TreeNode
(
int
x
)
:
val
(
x
),
left
(
NULL
),
right
(
NULL
)
{}
};
// Morris PostOrder Traversal
//
// Time Complexity: O(n)
// Space Complexity: O(1)
class
Solution
{
public:
vector
<
int
>
postorderTraversal
(
TreeNode
*
root
)
{
vector
<
int
>
res
;
if
(
root
==
NULL
)
return
res
;
TreeNode
*
dummyRoot
=
new
TreeNode
(
-
1
);
dummyRoot
->
left
=
root
;
TreeNode
*
cur
=
dummyRoot
;
while
(
cur
!=
NULL
){
if
(
cur
->
left
==
NULL
)
cur
=
cur
->
right
;
else
{
TreeNode
*
prev
=
cur
->
left
;
while
(
prev
->
right
!=
NULL
&&
prev
->
right
!=
cur
)
prev
=
prev
->
right
;
if
(
prev
->
right
==
NULL
){
prev
->
right
=
cur
;
cur
=
cur
->
left
;
}
else
{
prev
->
right
=
NULL
;
reverseTraversal
(
cur
->
left
,
res
);
cur
=
cur
->
right
;
}
}
}
delete
dummyRoot
;
return
res
;
}
private:
void
reverseTraversal
(
TreeNode
*
node
,
vector
<
int
>&
res
){
int
start
=
res
.
size
();
while
(
node
!=
NULL
){
res
.
push_back
(
node
->
val
);
node
=
node
->
right
;
}
reverse
(
res
.
begin
()
+
start
,
res
.
end
());
}
};
void
print_vec
(
const
vector
<
int
>&
vec
){
for
(
int
e
:
vec
)
cout
<<
e
<<
" "
;
cout
<<
endl
;
}
int
main
()
{
TreeNode
*
root
=
new
TreeNode
(
1
);
root
->
right
=
new
TreeNode
(
2
);
root
->
right
->
left
=
new
TreeNode
(
3
);
print_vec
(
Solution
().
postorderTraversal
(
root
));
return
0
;
}
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution1.java
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
import
java.util.ArrayList
;
import
java.util.List
;
// Recursive
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
public
class
Solution1
{
public
List
<
Integer
>
postorderTraversal
(
TreeNode
root
)
{
ArrayList
<
Integer
>
res
=
new
ArrayList
<
Integer
>();
postorderTraversal
(
root
,
res
);
return
res
;
}
private
void
postorderTraversal
(
TreeNode
node
,
List
<
Integer
>
list
){
if
(
node
!=
null
){
postorderTraversal
(
node
.
left
,
list
);
postorderTraversal
(
node
.
right
,
list
);
list
.
add
(
node
.
val
);
}
}
}
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution2.java
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
// My Non-Recursive
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
public
class
Solution2
{
private
class
Command
{
String
s
;
// go, print
TreeNode
node
;
Command
(
String
s
,
TreeNode
node
){
this
.
s
=
s
;
this
.
node
=
node
;
}
};
public
List
<
Integer
>
postorderTraversal
(
TreeNode
root
)
{
ArrayList
<
Integer
>
res
=
new
ArrayList
<
Integer
>();
if
(
root
==
null
)
return
res
;
Stack
<
Command
>
stack
=
new
Stack
<
Command
>();
stack
.
push
(
new
Command
(
"go"
,
root
));
while
(!
stack
.
empty
()){
Command
command
=
stack
.
pop
();
if
(
command
.
s
.
equals
(
"print"
))
res
.
add
(
command
.
node
.
val
);
else
{
assert
command
.
s
.
equals
(
"go"
);
stack
.
push
(
new
Command
(
"print"
,
command
.
node
));
if
(
command
.
node
.
right
!=
null
)
stack
.
push
(
new
Command
(
"go"
,
command
.
node
.
right
));
if
(
command
.
node
.
left
!=
null
)
stack
.
push
(
new
Command
(
"go"
,
command
.
node
.
left
));
}
}
return
res
;
}
}
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution3.java
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-30
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
// Non-Recursive
// Using a tag to record whether the node has been visited
//
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
public
class
Solution3
{
private
class
TagNode
{
TreeNode
node
;
boolean
isFirst
;
TagNode
(
TreeNode
node
){
this
.
node
=
node
;
this
.
isFirst
=
false
;
}
};
public
List
<
Integer
>
postorderTraversal
(
TreeNode
root
)
{
ArrayList
<
Integer
>
res
=
new
ArrayList
<
Integer
>();
if
(
root
==
null
)
return
res
;
Stack
<
TagNode
>
stack
=
new
Stack
<>();
TreeNode
cur
=
root
;
while
(
cur
!=
null
||
!
stack
.
empty
()){
while
(
cur
!=
null
){
stack
.
push
(
new
TagNode
(
cur
));
cur
=
cur
.
left
;
}
TagNode
tagNode
=
stack
.
pop
();
cur
=
tagNode
.
node
;
if
(
tagNode
.
isFirst
==
false
){
tagNode
.
isFirst
=
true
;
stack
.
push
(
tagNode
);
cur
=
cur
.
right
;
}
else
{
res
.
add
(
cur
.
val
);
cur
=
null
;
}
}
return
res
;
}
}
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution4.java
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-30
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
// Non-Recursive
// Using two stacks, Reverse the Preorder Traversal!
//
// Time Complexity: O(n)
// Space Complexity: O(n)
public
class
Solution4
{
public
List
<
Integer
>
postorderTraversal
(
TreeNode
root
)
{
ArrayList
<
Integer
>
res
=
new
ArrayList
<
Integer
>();
if
(
root
==
null
)
return
res
;
Stack
<
TreeNode
>
stack
=
new
Stack
<>();
Stack
<
Integer
>
output
=
new
Stack
<>();
stack
.
push
(
root
);
while
(!
stack
.
empty
()){
TreeNode
cur
=
stack
.
pop
();
output
.
push
(
cur
.
val
);
if
(
cur
.
left
!=
null
)
stack
.
push
(
cur
.
left
);
if
(
cur
.
right
!=
null
)
stack
.
push
(
cur
.
right
);
}
while
(!
output
.
empty
())
res
.
add
(
output
.
pop
());
return
res
;
}
}
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution5.java
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-30
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
import
java.util.LinkedList
;
// Non-Recursive
// Using two stacks, Reverse the Preorder Traversal!
//
// Time Complexity: O(n)
// Space Complexity: O(n)
public
class
Solution5
{
public
List
<
Integer
>
postorderTraversal
(
TreeNode
root
){
Stack
<
TreeNode
>
stack
=
new
Stack
<>();
LinkedList
<
TreeNode
>
output
=
new
LinkedList
<>();
TreeNode
p
=
root
;
while
(
p
!=
null
||
!
stack
.
isEmpty
()){
if
(
p
!=
null
){
stack
.
push
(
p
);
output
.
push
(
p
);
p
=
p
.
right
;
}
else
{
p
=
stack
.
pop
();
p
=
p
.
left
;
}
}
List
<
Integer
>
res
=
new
ArrayList
<>();
while
(!
output
.
isEmpty
())
res
.
add
(
output
.
pop
().
val
);
return
res
;
}
}
Prev
1
2
3
4
5
6
7
Next
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment