94. In-order traversal of binary tree (java implementation) --LeetCode

topic:

Given the root node root of a binary tree, return its mid-order traversal.

Example 1:
Insert picture description here

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

Example 2:

输入:root = []
输出:[]

Example 3:

输入:root = [1]
输出:[1]

Example 4:
Insert picture description here

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

Example 5:

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

prompt:

  • The number of nodes in the tree is in the range [0, 100]
  • -100 <= Node.val <= 100

Advanced: Recursive algorithm is very simple, can you do it through iterative algorithm?

Solution 1: Recursion

    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);
    }

Time complexity: On

Space complexity: On
Insert picture description here

Solution 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;
    }

Time complexity: On

Space complexity: On
Insert picture description here

Solution 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;
    }

Time complexity: On

Space complexity: On
Insert picture description here

Solution 4: Color mark

/**
 * 思路:
 * 颜色标记,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;
    }

Time complexity: On

Space complexity: On
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_38783664/article/details/111773856