zoukankan      html  css  js  c++  java
  • 94-二叉树的中序遍历

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

    示例 1:


    输入:root = [1,null,2,3]
    输出:[1,3,2]
    示例 2:

    输入:root = []
    输出:[]
    示例 3:

    输入:root = [1]
    输出:[1]
    示例 4:


    输入:root = [1,2]
    输出:[2,1]
    示例 5:


    输入:root = [1,null,2]
    输出:[1,2]
     

    提示:

    树中节点数目在范围 [0, 100] 内
    -100 <= Node.val <= 100

    解法一:递归求解 

    package suanfa;
    
    import java.util.ArrayList;
    import java.util.List;
    
    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;
        }
    }
    
    public class Solution {
        public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> res = new ArrayList<Integer>();
            inorder(root,res);
            return res;
    
        }
        void inorder(TreeNode root,List<Integer> res){
            if(root==null) {
                return ;
            }
            inorder(root.left,res);
            res.add(root.val);
            inorder(root.right,res);
        }
    
        public static void main(String[] args) {
            // TODO 自动生成的方法存根
    
        }
    
    }

    解法二:迭代求解

    递归本身包含着隐式求解,中序遍历是将递归过程转换成while循环,取中间值,转换递归方向,时间复杂度为O(n),n为树的个数,空间复杂度为O(N)为N的深度

    public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> res = new ArrayList<Integer>();
            Deque<TreeNode> stk = new LinkedList<TreeNode>();
            while (root != null || !stk.isEmpty()) {
                while (root != null) {
                    stk.push(root);
                    root = root.left;
                }
                if (!stk.isEmpty()) {
                    root = stk.pop();
                    res.add(root.val);
                    root = root.right;
                }
    
            }
            return res;
        }

    解法三:Morris 遍历算法  有点像线索二叉树,原理是一样的,

    Morris 遍历算法是另一种遍历二叉树的方法,它能将非递归的中序遍历空间复杂度降为 O(1)O(1)。

    Morris 遍历算法整体步骤如下(假设当前遍历到的节点为 xx):

    如果 xx 无左孩子,先将 xx 的值加入答案数组,再访问 xx 的右孩子,即 x = x. extit{right}x=x.right。
    如果 xx 有左孩子,则找到 xx 左子树上最右的节点(即左子树中序遍历的最后一个节点,xx 在中序遍历中的前驱节点),我们记为 extit{predecessor}predecessor。根据 extit{predecessor}predecessor 的右孩子是否为空,进行如下操作。
    如果 extit{predecessor}predecessor 的右孩子为空,则将其右孩子指向 xx,然后访问 xx 的左孩子,即 x = x. extit{left}x=x.left。
    如果 extit{predecessor}predecessor 的右孩子不为空,则此时其右孩子指向 xx,说明我们已经遍历完 xx 的左子树,我们将 extit{predecessor}predecessor 的右孩子置空,将 xx 的值加入答案数组,然后访问 xx 的右孩子,即 x = x. extit{right}x=x.right。
    重复上述操作,直至访问完整棵树。

    public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> res = new ArrayList<Integer>();
            TreeNode pre;
            while(root!=null) {
                if(root.left!=null) {
                    pre = root.left;
                    while(pre.right!=null) {
                        pre = pre.right;
                    }
                    TreeNode temp = root.left;
                    root.left = null;
                    pre.right = root;
                    root = temp;
                }else {
                    res.add(root.val);
                    root = root.right;
                }
            }
            return res;
        }
  • 相关阅读:
    ZJOI2019 Day1 游记
    Codeforces Round #545 (Div. 1)
    AtCoder WTF 2019 C2. Triangular Lamps Hard
    析合树
    Codeforces Round #539 (Div. 1)
    Codeforces 1103 E. Radix sum
    Codeforces 1097 G. Vladislav and a Great Legend
    sts创建spring项目
    servlet项目demo
    IDEA中创建maven web项目
  • 原文地址:https://www.cnblogs.com/xiaoming521/p/14860654.html
Copyright © 2011-2022 走看看