从根节点往下查找,先找左子树、直至左子树为空(左子节点逐个入栈、直至左子节点为空),再找右子
    树(出栈找右子节点)
    前序遍历:根左右,第一次经过节点即打印,直到打印null,往回溯,打印右子树
    中序遍历:左根右,第二次经过该节点时进行打印,即左边回溯时
    后序遍历:左右根,第三次经过该节点时进行打印,即右边回溯时
    层序遍历:按照层级,从上往下,从左到右。使用广度优先搜索算法。

    递归遍历:

    1. public static void preorder(TreeNode root) {
    2. if (root == null) {
    3. return;
    4. }
    5. //System.out.println(root.val);//前序 第一次成为栈顶
    6. preorder(root.left);
    7. System.out.println(root.val);
    8. //中序 第二次成为栈顶
    9. preorder(root.right);
    10. //System.out.println(root.val);//后序 第三次成为栈顶
    11. }

    迭代遍历:

    1. //前序:使用stack记录递归路径,左子节点后添加保证先出栈
    2. public static void preOrder2(TreeNode head) {
    3. if (head != null) {
    4. Stack<TreeNode> stack = new Stack<TreeNode>();
    5. stack.add(head);
    6. while (!stack.isEmpty()) {
    7. head = stack.pop();
    8. if (head != null) {
    9. System.out.println(head.val);
    10. stack.push(head.right);
    11. stack.push(head.left);
    12. }
    13. }
    14. }
    15. }
    16. //中序:将左子节点入栈,出栈打印值,然后添加右子节点
    17. public static void preOrder3(TreeNode head) {
    18. if (head != null) {
    19. Stack<TreeNode> stack = new Stack<TreeNode>();
    20. while (!stack.isEmpty() || (head != null)) {
    21. if (head != null) {
    22. stack.push(head);
    23. head = head.left;
    24. } else {
    25. head = stack.pop();
    26. System.out.println(head.val);
    27. head = head.right;
    28. }
    29. }
    30. }
    31. }
    32. //后序:
    33. public static void postorderTraversal(TreeNode root) {
    34. if (root == null) {
    35. return;
    36. }
    37. Deque<TreeNode> stack = new LinkedList<TreeNode>();
    38. TreeNode prev = null;
    39. while ((root != null) || !stack.isEmpty()) {
    40. while (root != null) {
    41. stack.push(root);
    42. root = root.left;
    43. }
    44. root = stack.pop();
    45. //root的左子节点为null
    46. if ((root.right == null) || (root.right == prev)) {
    47. //右子节点为null,或者右子节 点已打印
    48. System.out.println(root.val);
    49. prev = root;
    50. root = null;
    51. } else {
    52. //右子节点有值,重新入栈
    53. stack.push(root);
    54. root = root.right;
    55. }
    56. }
    57. }

    层序遍历:

    1. public static void levelTraversal(Node root) {
    2. Queue<Node> q = new LinkedList<>();
    3. q.add(root);
    4. while (!q.isEmpty()) {
    5. Node temp = q.poll();
    6. if (temp != null) {
    7. System.out.print(temp.value + " ");
    8. q.add(temp.left);
    9. q.add(temp.right);
    10. }
    11. }
    12. }
    13. public static void deepOrder(TreeNode root) {
    14. if (root == null) {
    15. return;
    16. }
    17. Queue<TreeNode> queue = new LinkedList<TreeNode>();
    18. queue.offer(root);
    19. while (!queue.isEmpty()) {
    20. for (int i = 1; i <= queue.size(); ++i) {
    21. TreeNode node = queue.poll();
    22. System.out.println(node.val);
    23. if (node.left != null) {
    24. queue.offer(node.left);
    25. }
    26. if (node.right != null) {
    27. queue.offer(node.right);
    28. }
    29. }
    30. }
    31. }
    32. private static List order(TreeNode root, int i, ArrayList list) {
    33. if (root == null) {
    34. return null;
    35. }
    36. int length = list.size();
    37. if (length <= i) {
    38. for (int j = 0; j <= (i - length); j++) {
    39. list.add(length + j, null);
    40. }
    41. }
    42. list.set(i, root.val);
    43. order(root.left, 2 * i, list);
    44. order(root.right, (2 * i) + 1, list);
    45. return list;
    46. }

    线索二叉树:
    在N个节点的二叉树中,每个节点有2个指针,所以一共有2N个指针,除了根节点以外,每一个节点都
    有一个指针从它的父节点指向它,所以一共使用了N-1个指针,所以剩下2N-(N-1)也就是N+1个空指
    针;
    如果能利用这些空指针域来存放指向该节点的直接前驱或是直接后继的指针,则可由此信息直接找到在
    该遍历次序下的前驱节点或后继节点,从而比递归遍历提高了遍历速度,节省了建立系统递归栈所使用
    的存储空间;
    这些被重新利用起来的空指针就被称为线索(Thread),加上了线索的二叉树就是线索二叉树
    实现思路:按某种次序遍历二叉树,在遍历过程中用线索取代空指针即可。以中序遍历为例,首先找到
    中序遍历的开始节点,然后利用线索依次查找后继节点即可。
    由于它充分利用了空指针域的空间(等于节省了空间),又保证了创建时的一次遍历就可以终生受用前
    驱、后继的信息(这意味着节省了时间),所以在实际问题中,如果所使用的二叉树需要经常遍历或查
    找节点时需要某种遍历中的前驱和后继,那么采用线索二叉链表的存储结构就是不错的选择

    morris遍历:构建中序线索二叉树的过程中,如果发现前驱节点的右指针指向自身,则将指针(线索)
    删除

    1. public static void morrisPre(Node cur) {
    2. if (head == null) {
    3. return;
    4. }
    5. Node mostRight = null;
    6. while (cur != null) {
    7. // cur表示当前节点,mostRight表示cur的左孩子的最右节点
    8. mostRight = cur.left;
    9. if (mostRight != null) {
    10. // cur有左孩子,找到cur左子树最右节点
    11. while ((mostRight.right != null) && (mostRight.right != cur)) {
    12. mostRight = mostRight.right;
    13. }
    14. // mostRight的右孩子指向空,让其指向cur,cur向左移动
    15. if (mostRight.right == null) {
    16. mostRight.right = cur;
    17. System.out.print(cur.value + " ");
    18. cur = cur.left;
    19. continue;
    20. } else {
    21. // mostRight的右孩子指向cur,让其指向空,cur向右移动
    22. mostRight.right = null;
    23. }
    24. } else {
    25. System.out.print(cur.value + " ");
    26. }
    27. cur = cur.right;
    28. }
    29. }
    30. public static void morrisIn(Node cur) {
    31. if (head == null) {
    32. return;
    33. }
    34. Node mostRight = null;
    35. while (cur != null) {
    36. mostRight = cur.left;
    37. if (mostRight != null) {
    38. while ((mostRight.right != null) && (mostRight.right != cur)) {
    39. mostRight = mostRight.right;
    40. }
    41. if (mostRight.right == null) {
    42. mostRight.right = cur;
    43. cur = cur.left;
    44. continue;
    45. } else {
    46. mostRight.right = null;
    47. }
    48. }
    49. System.out.print(cur.value + " ");
    50. cur = cur.right;
    51. }
    52. }
    53. public static void morrisPos(TreeNode cur) {
    54. if (cur == null) {
    55. return;
    56. }
    57. TreeNode head = cur;
    58. TreeNode mostRight = null;
    59. while (cur != null) {
    60. mostRight = cur.left;
    61. if (mostRight != null) {
    62. while ((mostRight.right != null) && (mostRight.right != cur)) {
    63. mostRight = mostRight.right;
    64. }
    65. if (mostRight.right == null) {
    66. mostRight.right = cur;
    67. cur = cur.left;
    68. continue;
    69. } else {
    70. mostRight.right = null;
    71. printEdge(cur.left);
    72. }
    73. }
    74. cur = cur.right;
    75. }
    76. printEdge(head);
    77. System.out.println();
    78. }
    79. public static void printEdge(TreeNode head) {
    80. TreeNode tail = reverseEdge(head);
    81. TreeNode cur = tail;
    82. while (cur != null) {
    83. System.out.print(cur.val + " ");
    84. cur = cur.right;
    85. }
    86. reverseEdge(tail);
    87. }
    88. public static TreeNode reverseEdge(TreeNode from) {
    89. TreeNode pre = null;
    90. TreeNode next = null;
    91. while (from != null) {
    92. next = from.right;
    93. from.right = pre;
    94. pre = from;
    95. from = next;
    96. }
    97. return pre;
    98. }
    99. public List<Integer> postorderTraversal(TreeNode root) {
    100. List<Integer> res = new ArrayList<Integer>();
    101. if (root == null) {
    102. return res;
    103. }
    104. Deque<TreeNode> stack = new LinkedList<TreeNode>();
    105. TreeNode prev = null;
    106. while ((root != null) || !stack.isEmpty()) {
    107. while (root != null) {
    108. stack.push(root);
    109. root = root.left;
    110. }
    111. root = stack.pop();
    112. if ((root.right == null) || (root.right == prev)) {
    113. res.add(root.val);
    114. prev = root;
    115. root = null;
    116. } else {
    117. stack.push(root);
    118. root = root.right;
    119. }
    120. }
    121. return res;
    122. }