递归的思路非常简单,只需调整打印位置即可
    非递归思路
    (1)前序遍历
    使用一个栈,先将根节点放入栈,然后出栈,然后分别按照 右节点、左节点的顺序放入栈,继续循环出栈
    (2)中序遍历
    使用一个栈,先不断的遍历最左侧的节点放入栈,如果左边为null了则出栈null的父节点,然后将父节点右子树做相同的操作,最后直到栈为空
    (3)后序遍历
    使用两个栈,先将根节点入栈,然后出栈并放入栈2,然后放入左节点,右节点然后再出栈放入栈2,最后将栈2出栈打印即可

    1. import java.util.*;
    2. /*
    3. public class TreeNode {
    4. int val = 0;
    5. TreeNode left = null;
    6. TreeNode right = null;
    7. public TreeNode(int val) {
    8. this.val = val;
    9. }
    10. }*/
    11. public class TreeToSequence {
    12. public int[][] convert(TreeNode root) {
    13. List<Integer> preResult = new ArrayList<>();
    14. List<Integer> midResult = new ArrayList<>();
    15. List<Integer> afterResult = new ArrayList<>();
    16. prePrint(preResult, root);
    17. midPrint(midResult, root);
    18. afterPrint(afterResult, root);
    19. int[][] result = new int[3][preResult.size()];
    20. for (int i=0; i< preResult.size(); i++) {
    21. result[0][i] = preResult.get(i);
    22. }
    23. for (int i=0; i< midResult.size(); i++) {
    24. result[1][i] = midResult.get(i);
    25. }
    26. for (int i=0; i< afterResult.size(); i++) {
    27. result[2][i] = afterResult.get(i);
    28. }
    29. return result;
    30. }
    31. private void prePrint(List<Integer> preResult,TreeNode node) {
    32. if (node == null) {
    33. return;
    34. }
    35. Stack<TreeNode> stack = new Stack<TreeNode>();
    36. stack.push(node);
    37. while (!stack.isEmpty()) {
    38. TreeNode pop = stack.pop();
    39. preResult.add(pop.val);
    40. if (pop.right != null) {
    41. stack.push(pop.right);
    42. }
    43. if (pop.left != null) {
    44. stack.push(pop.left);
    45. }
    46. }
    47. }
    48. private void midPrint(List<Integer> midResult, TreeNode node) {
    49. if (node == null) {
    50. return;
    51. }
    52. Stack<TreeNode> stack = new Stack<TreeNode>();
    53. stack.push(node);
    54. TreeNode cur = node;
    55. while (!stack.isEmpty()) {
    56. if (cur != null) {
    57. if (cur.left != null) {
    58. stack.push(cur.left);
    59. cur = cur.left;
    60. } else {
    61. cur = null;
    62. }
    63. } else {
    64. TreeNode pop = stack.pop();
    65. midResult.add(pop.val);
    66. if (pop.right != null) {
    67. stack.push(pop.right);
    68. cur = pop.right;
    69. } else {
    70. cur = null;
    71. }
    72. }
    73. }
    74. }
    75. private void afterPrint(List<Integer> afterResult, TreeNode node) {
    76. if (node == null) {
    77. return;
    78. }
    79. Stack<TreeNode> s1 = new Stack<TreeNode>();
    80. Stack<Integer> s2 = new Stack<Integer>();
    81. s1.push(node);
    82. while (!s1.isEmpty()) {
    83. TreeNode pop = s1.pop();
    84. s2.push(pop.val);
    85. if (pop.left != null) {
    86. s1.push(pop.left);
    87. }
    88. if (pop.right != null) {
    89. s1.push(pop.right);
    90. }
    91. }
    92. while (!s2.isEmpty()) {
    93. afterResult.add(s2.pop());
    94. }
    95. }
    96. /** 下面为递归方式 **
    97. private void prePrint(List<Integer> preResult,TreeNode node) {
    98. if (node == null) {
    99. return;
    100. }
    101. preResult.add(node.val);
    102. prePrint(preResult, node.left);
    103. prePrint(preResult, node.right);
    104. }
    105. private void midPrint(List<Integer> midResult, TreeNode node) {
    106. if (node == null) {
    107. return;
    108. }
    109. midPrint(midResult, node.left);
    110. midResult.add(node.val);
    111. midPrint(midResult, node.right);
    112. }
    113. private void afterPrint(List<Integer> afterResult, TreeNode node) {
    114. if (node == null) {
    115. return;
    116. }
    117. afterPrint(afterResult, node.left);
    118. afterPrint(afterResult, node.right);
    119. afterResult.add(node.val);
    120. }
    121. **/
    122. }

    image.gif