File tree Expand file tree Collapse file tree 4 files changed +72
-8
lines changed Expand file tree Collapse file tree 4 files changed +72
-8
lines changed Original file line number Diff line number Diff line change 1
- import java .util .ArrayDeque ;
2
- import java .util .Deque ;
3
- import java .util .LinkedList ;
4
- import java .util .List ;
1
+ import java .util .*;
5
2
6
3
public class BinaryTreePostorderTraversal {
7
4
@@ -25,4 +22,67 @@ public List<Integer> postorderTraversal(TreeNode root) {
25
22
}
26
23
return results ;
27
24
}
25
+
26
+
27
+ public ArrayList <Integer > postorderTraversal2 (TreeNode root ) {
28
+ ArrayList <Integer > result = new ArrayList <Integer >();
29
+ Stack <TreeNode > stack = new Stack <TreeNode >();
30
+ TreeNode prev = null ; // previously traversed node
31
+ TreeNode curr = root ;
32
+
33
+ if (root == null ) {
34
+ return result ;
35
+ }
36
+
37
+ stack .push (root );
38
+ while (!stack .empty ()) {
39
+ curr = stack .peek ();
40
+ if (prev == null || prev .left == curr || prev .right == curr ) { // traverse down the tree
41
+ if (curr .left != null ) {
42
+ stack .push (curr .left );
43
+ } else if (curr .right != null ) {
44
+ stack .push (curr .right );
45
+ }
46
+ } else if (curr .left == prev ) { // traverse up the tree from the left
47
+ if (curr .right != null ) {
48
+ stack .push (curr .right );
49
+ }
50
+ } else { // traverse up the tree from the right
51
+ result .add (curr .val );
52
+ stack .pop ();
53
+ }
54
+ prev = curr ;
55
+ }
56
+
57
+ return result ;
58
+ }
59
+
60
+ public List <Integer > postorderTraversal3 (TreeNode root ) {
61
+ List <Integer > result = new ArrayList <>();
62
+
63
+ if (root == null ) {
64
+ return result ;
65
+ }
66
+
67
+ Stack <TreeNode > stack = new Stack <>();
68
+
69
+ TreeNode last = null ;
70
+
71
+ while (!stack .isEmpty () || root != null ) {
72
+ if (root != null ) {
73
+ stack .push (root );
74
+ root = root .left ;
75
+ } else {
76
+ TreeNode peek = stack .peek ();
77
+ if (peek .right != null && last != peek .right ) {
78
+ root = peek .right ;
79
+ } else {
80
+ result .add (peek .val );
81
+ last = stack .pop ();
82
+ }
83
+ }
84
+ }
85
+
86
+ return result ;
87
+ }
28
88
}
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ public class BinaryTreeUpsideDown {
3
3
/**
4
4
* 将root的左子树看作一个整体,root.left就作为倒置后的root和root.right的新parent
5
5
* 注意若root的左子树为空,则无法倒置了,什么也不做
6
+ * 注意最后要给root的左右清零
6
7
*/
7
8
public TreeNode upsideDownBinaryTree (TreeNode root ) {
8
9
if (root == null || root .left == null ) {
Original file line number Diff line number Diff line change @@ -50,7 +50,7 @@ private TreeNode helper(Queue<String> queue) {
50
50
return root ;
51
51
}
52
52
53
- /** 下面是非递归版的DFS */
53
+ /** 下面是非递归版的,前序遍历 */
54
54
public String serialize2 (TreeNode root ) {
55
55
StringBuilder sb = new StringBuilder ();
56
56
if (root == null ) {
@@ -74,7 +74,10 @@ public String serialize2(TreeNode root) {
74
74
return sb .toString ();
75
75
}
76
76
77
- // Decodes your encoded data to tree.
77
+ /**
78
+ * 前序访问一遍所有结点
79
+ * 在设置node时,从queue中取出值
80
+ */
78
81
public TreeNode deserialize2 (String data ) {
79
82
String [] texts = data .split (SEP );
80
83
Queue <String > queue = new LinkedList <String >(Arrays .asList (texts ));
Original file line number Diff line number Diff line change @@ -6,12 +6,12 @@ public class Main {
6
6
7
7
public static class Solution {
8
8
9
+ public int sumOfLeftLeaves (TreeNode root ) {
9
10
10
-
11
+ }
11
12
}
12
13
13
14
public static void main (String [] args ) {
14
15
Solution s = new Solution ();
15
- System .out .println (a );
16
16
}
17
17
}
You can’t perform that action at this time.
0 commit comments