File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree 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 .*;
52
63public class BinaryTreePostorderTraversal {
74
@@ -25,4 +22,67 @@ public List<Integer> postorderTraversal(TreeNode root) {
2522 }
2623 return results ;
2724 }
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+ }
2888}
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ public class BinaryTreeUpsideDown {
33 /**
44 * 将root的左子树看作一个整体,root.left就作为倒置后的root和root.right的新parent
55 * 注意若root的左子树为空,则无法倒置了,什么也不做
6+ * 注意最后要给root的左右清零
67 */
78 public TreeNode upsideDownBinaryTree (TreeNode root ) {
89 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) {
5050 return root ;
5151 }
5252
53- /** 下面是非递归版的DFS */
53+ /** 下面是非递归版的,前序遍历 */
5454 public String serialize2 (TreeNode root ) {
5555 StringBuilder sb = new StringBuilder ();
5656 if (root == null ) {
@@ -74,7 +74,10 @@ public String serialize2(TreeNode root) {
7474 return sb .toString ();
7575 }
7676
77- // Decodes your encoded data to tree.
77+ /**
78+ * 前序访问一遍所有结点
79+ * 在设置node时,从queue中取出值
80+ */
7881 public TreeNode deserialize2 (String data ) {
7982 String [] texts = data .split (SEP );
8083 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 {
66
77 public static class Solution {
88
9+ public int sumOfLeftLeaves (TreeNode root ) {
910
10-
11+ }
1112 }
1213
1314 public static void main (String [] args ) {
1415 Solution s = new Solution ();
15- System .out .println (a );
1616 }
1717}
You can’t perform that action at this time.
0 commit comments