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

Commitdc8725f

Browse files
author
applewjg
committed
Binary Tree Postorder Traversal
Change-Id: I14aeed7075fb90bace971602710e62155f2136d6
1 parent02b4f8c commitdc8725f

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

‎BinaryTreePostorderTraversal.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Nov 20, 2014
4+
Problem: Binary Tree Postorder Traversal
5+
Difficulty: Easy
6+
Source: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/
7+
Notes:
8+
Given a binary tree, return the postorder traversal of its nodes' values.
9+
10+
For example:
11+
Given binary tree {1,#,2,3},
12+
1
13+
\
14+
2
15+
/
16+
3
17+
return [3,2,1].
18+
19+
Note: Recursive solution is trivial, could you do it iteratively?
20+
21+
Solution: 1. Iterative way (stack). Time: O(n), Space: O(n).
22+
2. Recursive solution. Time: O(n), Space: O(n).
23+
3. Threaded tree (Morris). Time: O(n), Space: O(n/1).
24+
Space: O(1) if in-place reverse.
25+
You may refer to my blog for more detailed explanations:
26+
http://www.cnblogs.com/AnnieKim/archive/2013/06/15/MorrisTraversal.html
27+
*/
28+
29+
30+
/**
31+
* Definition for binary tree
32+
* public class TreeNode {
33+
* int val;
34+
* TreeNode left;
35+
* TreeNode right;
36+
* TreeNode(int x) { val = x; }
37+
* }
38+
*/
39+
publicclassSolution {
40+
publicList<Integer>postorderTraversal_1(TreeNoderoot) {
41+
List<Integer>res =newArrayList<Integer>();
42+
if (root ==null)returnres;
43+
Stack<TreeNode>stk =newStack<TreeNode>();
44+
TreeNodecur =root;
45+
TreeNodepre =null;
46+
while (stk.isEmpty() ==false ||cur !=null) {
47+
if (cur !=null) {
48+
stk.push(cur);
49+
cur =cur.left;
50+
}else {
51+
TreeNodepeak =stk.peek();
52+
if (peak.right !=null &&pre !=peak.right) {
53+
cur =peak.right;
54+
}else {
55+
res.add(peak.val);
56+
stk.pop();
57+
pre =peak;
58+
}
59+
}
60+
}
61+
returnres;
62+
}
63+
publicList<Integer>postorderTraversal_2(TreeNoderoot) {
64+
List<Integer>res =newArrayList<Integer>();
65+
if (root ==null)returnres;
66+
List<Integer>left =postorderTraversal(root.left);
67+
List<Integer>right =postorderTraversal(root.right);
68+
res.addAll(left);
69+
res.addAll(right);
70+
res.add(root.val);
71+
returnres;
72+
}
73+
publicList<Integer>postorderTraversal_3(TreeNoderoot) {
74+
List<Integer>res =newArrayList<Integer>();
75+
if (root ==null)returnres;
76+
Stack<Integer>stk =newStack<Integer>();
77+
TreeNodedummy =newTreeNode(-1);
78+
dummy.left =root;
79+
TreeNodecur =dummy;
80+
while (cur !=null) {
81+
if (cur.left ==null) {
82+
cur =cur.right;
83+
}else {
84+
TreeNodenode =cur.left;
85+
while (node.right !=null &&node.right !=cur)
86+
node =node.right;
87+
if (node.right ==null) {
88+
node.right =cur;
89+
cur =cur.left;
90+
}else {
91+
TreeNodetemp =cur.left;
92+
while (temp !=cur) {
93+
stk.push(temp.val);
94+
temp =temp.right;
95+
}
96+
while (stk.isEmpty() ==false)res.add(stk.pop());
97+
node.right =null;
98+
cur =cur.right;
99+
}
100+
}
101+
}
102+
returnres;
103+
}
104+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp