赞
踩
1.题目描述:
给定一个二叉树的根节点root,返回它的中序遍历。
2.递归中序遍历:
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode() {}
- * TreeNode(int val) { this.val = val; }
- * TreeNode(int val, TreeNode left, TreeNode right) {
- * this.val = val;
- * this.left = left;
- * this.right = right;
- * }
- * }
- */
- class Solution {
- private List<Integer> list = new ArrayList<>();
- public List<Integer> inorderTraversal(TreeNode root) {
- inOrder(root);
- return list;
- }
- public void inOrder(TreeNode root) {
- if (root == null) return;
- inOrder(root.left);
- list.add(root.val);
- inOrder(root.right);
- }
- }

3.使用栈的迭代中序遍历:
中序遍历和之前的leetcode144. 二叉树的前序遍历,leetcode145. 二叉树的后序遍历代码格式不同,中序遍历顺序为left—>root—>right,遍历节点时访问的顺序(始终先通过root节点向下走)与要操作的节点顺序不同,而前序遍历要访问的顺序和要操作的顺序是一致的都是root节点,因此使用迭代的中序遍历,就需要借用指针的遍历来帮助访问节点,栈则用来操作节点上的元素。代码如下,对着代码走一遍是,记住即可。
- class Solution {
- public List<Integer> inorderTraversal(TreeNode root) {
- List<Integer> list = new ArrayList<>();
- Stack<TreeNode> stack = new Stack<>();
- TreeNode temp = root;
- while (temp != null || !stack.isEmpty()){
- if (temp != null){
- stack.push(temp);
- temp = temp.left;//left
- }else{
- temp = stack.pop();
- list.add(temp.val);//root
- temp = temp.right;//right
- }
- }
- return list;
- }
- }

4.前序遍历、中序遍历、后序遍历的统一迭代写法:
以中序遍历为例,无法解决访问节点(遍历节点)和处理节点(将元素放进结果集)不一致的情况。那我们就将访问的节点放入栈中,把要处理的节点也放入栈中但是要做标记,就是要处理的节点放入栈之后,紧接着放入一个空指针作为标记。 这种方法也可以叫做标记法。代码只需要改动中间if中的几行顺序,说实话也不好理解,见下。
- //中序遍历
- class Solution {
- public List<Integer> inorderTraversal(TreeNode root) {
- List<Integer> list = new LinkedList<>();
- Stack<TreeNode> stack = new Stack<>();
- if (root != null) stack.push(root);
- while (!stack.isEmpty()) {
- TreeNode node = stack.peek();//只取值不出栈
- if (node != null) {
- stack.pop();//将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
- if (node.right != null) stack.push(node.right);//添加右节点(空节点不入栈)
- stack.push(node);//添加中节点
- stack.push(null);//中节点访问过,但是还没有处理,加入空节点做为标记。
- if (node.left != null) stack.push(node.left);//添加左节点(空节点不入栈)
- } else {//只有遇到空节点的时候,才将下一个节点放进结果集
- stack.pop();//将空节点弹出
- node = stack.pop();//重新取出栈中元素
- list.add(node.val);//加入到结果集
- }
- }
- return list;
- }
- }
- //前序遍历
- class Solution {
- public List<Integer> inorderTraversal(TreeNode root) {
- List<Integer> list = new LinkedList<>();
- Stack<TreeNode> stack = new Stack<>();
- if (root != null) stack.push(root);
- while (!stack.isEmpty()) {
- TreeNode node = stack.peek();//只取值不出栈
- if (node != null) {
- stack.pop();//将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
- if (node.right != null) stack.push(node.right);//添加右节点(空节点不入栈)
- if (node.left != null) stack.push(node.left);//添加左节点(空节点不入栈)
- stack.push(node);//添加中节点
- stack.push(null);//中节点访问过,但是还没有处理,加入空节点做为标记。
- } else {//只有遇到空节点的时候,才将下一个节点放进结果集
- stack.pop();//将空节点弹出
- node = stack.pop();//重新取出栈中元素
- list.add(node.val);//加入到结果集
- }
- }
- return list;
- }
- }
- //后序遍历
- class Solution {
- public List<Integer> inorderTraversal(TreeNode root) {
- List<Integer> list = new LinkedList<>();
- Stack<TreeNode> stack = new Stack<>();
- if (root != null) stack.push(root);
- while (!stack.isEmpty()) {
- TreeNode node = stack.peek();//只取值不出栈
- if (node != null) {
- stack.pop();//将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
- stack.push(node);//添加中节点
- stack.push(null);//中节点访问过,但是还没有处理,加入空节点做为标记。
- if (node.right != null) stack.push(node.right);//添加右节点(空节点不入栈)
- if (node.left != null) stack.push(node.left);//添加左节点(空节点不入栈)
- } else {//只有遇到空节点的时候,才将下一个节点放进结果集
- stack.pop();//将空节点弹出
- node = stack.pop();//重新取出栈中元素
- list.add(node.val);//加入到结果集
- }
- }
- return list;
- }
- }

5.Morris中序遍历:
与线索化二叉树有关系,线索化二叉树将前驱节点和后继节点通过线索连接。中序遍历只把后继节点线索化,假设当前遍历到的节点为root,将其左子树中最右节点的右子节点指向root,在左子树遍历完成后我们通过这个线索回到root,且可通过此线索来判断是否遍历完左子树(有左子树的节点在遍历过程中会访问两边,根据是否添加过线索来判断),而不用再通过栈来维护,达到空间复杂度为O(n)。
- class Solution {
- public List<Integer> inorderTraversal(TreeNode root) {
- List<Integer> list = new ArrayList<Integer>();
- TreeNode pre = null;
- while (root != null) {
- if (root.left != null) {
- pre = root.left;//找到左子树最右子节点pre
- while (pre.right != null && pre.right != root) {
- pre = pre.right;
- }
- if (pre.right == null) {//让pre的右指针指向root,继续遍历左子树
- pre.right = root;
- root = root.left;
- }
- else {//左子树访问完,抹去线索恢复原二叉树结构
- list.add(root.val);
- pre.right = null;
- root = root.right;
- }
- }
- else {//如果没有左子节点,则直接访问右子节点
- list.add(root.val);
- root = root.right;
- }
- }
- return list;
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。