94. 二叉树的中序遍历(java实现)--LeetCode

题目:

给定一个二叉树的根节点 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

进阶: 递归算法很简单,你可以通过迭代算法完成吗?

解法1:递归

    public List<Integer> inorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> result = new ArrayList<>();
        recursive(root,result);
        return result;
    }

    private void recursive(TreeNode root, ArrayList<Integer> result) {
    
    
        if (root==null)return;
        recursive(root.left,result);
        result.add(root.val);
        recursive(root.right,result);
    }

时间复杂度:On

空间复杂度:On
在这里插入图片描述

解法2:stack

/**
 * 思路:
 * 移动当前节点,入栈,直到走到最左。
 * 最左的值加入节点到结果集,之后继续移动当前节点走右边节点
 */
    public List<Integer> inorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> result = new ArrayList<>();
        ArrayDeque<TreeNode> stack = new ArrayDeque<>();
        while (root!=null||!stack.isEmpty()){
    
    
            while (root!=null){
    
    
                stack.push(root);
                root=root.left;
            }
            TreeNode pop = stack.pop();
            result.add(pop.val);
            root=pop.right;
        }
        return result;
    }

时间复杂度:On

空间复杂度:On
在这里插入图片描述

解法3:Morris

    public List<Integer> inorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> result = new ArrayList<>();
        while (root != null) {
    
    
            if (root.left != null) {
    
    
                TreeNode tail = root.left;
                while (tail.right != null && tail.right != root) tail = tail.right;
                if (tail.right==null){
    
    
                    tail.right=root;
                    root=root.left;
                }else {
    
    
                	//根据链表往回找的时候把值加入result
                    result.add(root.val);
                    root=root.right;
                }
            } else {
    
    
                result.add(root.val);
                root = root.right;
            }
        }
        return result;
    }

时间复杂度:On

空间复杂度:On
在这里插入图片描述

解法4:颜色标记

/**
 * 思路:
 * 颜色标记,0是没走过,1是走过
 * 在栈中存放paris 0/1,node
 * 如果当前节点是没走过,按照右根左的顺序加入栈中
 */
    public List<Integer> inorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> result = new ArrayList<>();
        if (root==null)return result;
        ArrayDeque<Pair<Integer,TreeNode>> stack = new ArrayDeque<>();
        stack.push(new Pair<>(0,root));
        while (!stack.isEmpty()){
    
    
            Pair<Integer, TreeNode> pop = stack.pop();
            if (pop.getKey()==0) {
    
    
                if (pop.getValue().right != null) stack.push(new Pair<>(0, pop.getValue().right));
                stack.push(new Pair<>(1, pop.getValue()));
                if (pop.getValue().left != null) stack.push(new Pair<>(0, pop.getValue().left));
            }else {
    
    
                result.add(pop.getValue().val);
            }
        }
        return result;
    }

时间复杂度:On

空间复杂度:On
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_38783664/article/details/111773856