当前位置:   article > 正文

leetcode94. 二叉树的中序遍历

94. 二叉树的中序遍历

1.题目描述:

给定一个二叉树的根节点root,返回它的中序遍历

 2.递归中序遍历:

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode() {}
  8. * TreeNode(int val) { this.val = val; }
  9. * TreeNode(int val, TreeNode left, TreeNode right) {
  10. * this.val = val;
  11. * this.left = left;
  12. * this.right = right;
  13. * }
  14. * }
  15. */
  16. class Solution {
  17. private List<Integer> list = new ArrayList<>();
  18. public List<Integer> inorderTraversal(TreeNode root) {
  19. inOrder(root);
  20. return list;
  21. }
  22. public void inOrder(TreeNode root) {
  23. if (root == null) return;
  24. inOrder(root.left);
  25. list.add(root.val);
  26. inOrder(root.right);
  27. }
  28. }

3.使用栈的迭代中序遍历:

中序遍历和之前的leetcode144. 二叉树的前序遍历leetcode145. 二叉树的后序遍历代码格式不同,中序遍历顺序为left—>root—>right,遍历节点时访问的顺序(始终先通过root节点向下走)与要操作的节点顺序不同,而前序遍历要访问的顺序和要操作的顺序是一致的都是root节点,因此使用迭代的中序遍历,就需要借用指针的遍历来帮助访问节点,栈则用来操作节点上的元素。代码如下,对着代码走一遍是,记住即可。

  1. class Solution {
  2. public List<Integer> inorderTraversal(TreeNode root) {
  3. List<Integer> list = new ArrayList<>();
  4. Stack<TreeNode> stack = new Stack<>();
  5. TreeNode temp = root;
  6. while (temp != null || !stack.isEmpty()){
  7. if (temp != null){
  8. stack.push(temp);
  9. temp = temp.left;//left
  10. }else{
  11. temp = stack.pop();
  12. list.add(temp.val);//root
  13. temp = temp.right;//right
  14. }
  15. }
  16. return list;
  17. }
  18. }

4.前序遍历、中序遍历、后序遍历的统一迭代写法:

以中序遍历为例,无法解决访问节点(遍历节点)和处理节点(将元素放进结果集)不一致的情况。那我们就将访问的节点放入栈中,把要处理的节点也放入栈中但是要做标记,就是要处理的节点放入栈之后,紧接着放入一个空指针作为标记。 这种方法也可以叫做标记法。代码只需要改动中间if中的几行顺序,说实话也不好理解,见下。

  1. //中序遍历
  2. class Solution {
  3. public List<Integer> inorderTraversal(TreeNode root) {
  4. List<Integer> list = new LinkedList<>();
  5. Stack<TreeNode> stack = new Stack<>();
  6. if (root != null) stack.push(root);
  7. while (!stack.isEmpty()) {
  8. TreeNode node = stack.peek();//只取值不出栈
  9. if (node != null) {
  10. stack.pop();//将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
  11. if (node.right != null) stack.push(node.right);//添加右节点(空节点不入栈)
  12. stack.push(node);//添加中节点
  13. stack.push(null);//中节点访问过,但是还没有处理,加入空节点做为标记。
  14. if (node.left != null) stack.push(node.left);//添加左节点(空节点不入栈)
  15. } else {//只有遇到空节点的时候,才将下一个节点放进结果集
  16. stack.pop();//将空节点弹出
  17. node = stack.pop();//重新取出栈中元素
  18. list.add(node.val);//加入到结果集
  19. }
  20. }
  21. return list;
  22. }
  23. }
  24. //前序遍历
  25. class Solution {
  26. public List<Integer> inorderTraversal(TreeNode root) {
  27. List<Integer> list = new LinkedList<>();
  28. Stack<TreeNode> stack = new Stack<>();
  29. if (root != null) stack.push(root);
  30. while (!stack.isEmpty()) {
  31. TreeNode node = stack.peek();//只取值不出栈
  32. if (node != null) {
  33. stack.pop();//将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
  34. if (node.right != null) stack.push(node.right);//添加右节点(空节点不入栈)
  35. if (node.left != null) stack.push(node.left);//添加左节点(空节点不入栈)
  36. stack.push(node);//添加中节点
  37. stack.push(null);//中节点访问过,但是还没有处理,加入空节点做为标记。
  38. } else {//只有遇到空节点的时候,才将下一个节点放进结果集
  39. stack.pop();//将空节点弹出
  40. node = stack.pop();//重新取出栈中元素
  41. list.add(node.val);//加入到结果集
  42. }
  43. }
  44. return list;
  45. }
  46. }
  47. //后序遍历
  48. class Solution {
  49. public List<Integer> inorderTraversal(TreeNode root) {
  50. List<Integer> list = new LinkedList<>();
  51. Stack<TreeNode> stack = new Stack<>();
  52. if (root != null) stack.push(root);
  53. while (!stack.isEmpty()) {
  54. TreeNode node = stack.peek();//只取值不出栈
  55. if (node != null) {
  56. stack.pop();//将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
  57. stack.push(node);//添加中节点
  58. stack.push(null);//中节点访问过,但是还没有处理,加入空节点做为标记。
  59. if (node.right != null) stack.push(node.right);//添加右节点(空节点不入栈)
  60. if (node.left != null) stack.push(node.left);//添加左节点(空节点不入栈)
  61. } else {//只有遇到空节点的时候,才将下一个节点放进结果集
  62. stack.pop();//将空节点弹出
  63. node = stack.pop();//重新取出栈中元素
  64. list.add(node.val);//加入到结果集
  65. }
  66. }
  67. return list;
  68. }
  69. }

5.Morris中序遍历:

与线索化二叉树有关系,线索化二叉树将前驱节点和后继节点通过线索连接。中序遍历只把后继节点线索化,假设当前遍历到的节点为root,将其左子树中最右节点的右子节点指向root,在左子树遍历完成后我们通过这个线索回到root,且可通过此线索来判断是否遍历完左子树(有左子树的节点在遍历过程中会访问两边,根据是否添加过线索来判断),而不用再通过栈来维护,达到空间复杂度为O(n)。

  1. class Solution {
  2. public List<Integer> inorderTraversal(TreeNode root) {
  3. List<Integer> list = new ArrayList<Integer>();
  4. TreeNode pre = null;
  5. while (root != null) {
  6. if (root.left != null) {
  7. pre = root.left;//找到左子树最右子节点pre
  8. while (pre.right != null && pre.right != root) {
  9. pre = pre.right;
  10. }
  11. if (pre.right == null) {//让pre的右指针指向root,继续遍历左子树
  12. pre.right = root;
  13. root = root.left;
  14. }
  15. else {//左子树访问完,抹去线索恢复原二叉树结构
  16. list.add(root.val);
  17. pre.right = null;
  18. root = root.right;
  19. }
  20. }
  21. else {//如果没有左子节点,则直接访问右子节点
  22. list.add(root.val);
  23. root = root.right;
  24. }
  25. }
  26. return list;
  27. }
  28. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/908005
推荐阅读
相关标签
  

闽ICP备14008679号