(Java) leetcode-94 Binary Tree Inorder Traversal (inorder traversal of three implementation)

topic

In the binary tree traversal sequence []
Given a binary tree, return the inorder traversal of its nodes' values.

Example:

Input: [1,null,2,3]

   1
    \
     2
    /
   3

Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?

Recursive thinking 1-

Recursive binary tree traversal is more conventional kind of approach.
Time complexity: O (n)
space complexity: worst O (n) average O (logn)

Code

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
        inorderVisit(root,list);
        return list;

    }

    public void inorderVisit(TreeNode root,List<Integer> list){
    	if(root != null){
    		inorderVisit(root.left,list);
    		list.add(root.val);
    		inorderVisit(root.right,list);
    	}
    }
}

Present the results

Runtime: 0 ms, faster than 100.00% of Java online submissions for Binary Tree Inorder Traversal.
Memory Usage: 36.2 MB, less than 40.53% of Java online submissions for Binary Tree Inorder Traversal.

Non-recursive idea 2-

It requires a combination of the stack to achieve. First turn the root of all the children left the stack, the stack when the leaf nodes, and access to the stack, then push it right child and continues to be the right child of the left child stack. . . Release cycling conditions were null pointer and the stack empty, it indicates the node traversed to the rightmost.
In fact, theory and recursive solution is about the same, but the stack into the system implemented by ourselves.
Time and space complexity is O (n)

Code

public class Solution {
    public List < Integer > inorderTraversal(TreeNode root) {
        List < Integer > res = new ArrayList < > ();
        Stack < TreeNode > stack = new Stack < > ();
        TreeNode curr = root;
        while (curr != null || !stack.isEmpty()) {
            while (curr != null) {
                stack.push(curr);
                curr = curr.left;
            }
            curr = stack.pop();
            res.add(curr.val);
            curr = curr.right;
        }
        return res;
    }
}

Present the results

Runtime: 1 ms, faster than 35.18% of Java online submissions for Binary Tree Inorder Traversal.
Memory Usage: 36.3 MB, less than 12.51% of Java online submissions for Binary Tree Inorder Traversal.

Ideas 3- Morris solution (threaded binary)

1, if the current left child node is empty, the current output node and a right node of the current node as the current node.
2, if the current node's left child is not empty, from the current node's left subtree find the current node precursor nodes:
If the predecessor node p of the right child to the right child is empty, then the p the current node; otherwise, the output current node and the right child of p is set to null, and the current right child of the current node is set to the current node
3. repeat 1, 2-step until the current node is empty
Here Insert Picture Description

Time complexity: O (n).
Space complexity: O (1), because only two auxiliary pointers.
Reference: http://www.cnblogs.com/AnnieKim/archive/2013/06/15/morristraversal.html

Code

public class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        if(root == null) return new ArrayList<Integer>();
        List<Integer> res = new ArrayList<Integer>();
        TreeNode pre = null;
        while(root != null){
        	if(root.left == null){//如果当前结点的左孩子为空
        		res.add(root.val);//则输出当前结点
        		root = root.right;//并将当前结点的右结点作为当前结点
        	}else{//如果当前结点的左孩子不为空,则从当前结点的左子树找出当前结点的前驱节点: 
        		pre = root.left;
        		while(pre.right != null && pre.right != root){
        			pre = pre.right;//最后pre指向当前结点的前驱结点
        		}
        		if(pre.right == null){//如果前驱结点p的右孩子为空,则将p的右孩子设为当前结点
        			pre.right = root;
        			root = root.left;
        		}else{//否则,输出当前结点,并将p的右孩子置为空,并将当前当前结点的右孩子置为当前结点
        			pre.right = null;
        			res.add(root.val);
        			root = root.right;
        		}
        	}
        }
        return res;
    }
}

Present the results

Runtime: 0 ms, faster than 100.00% of Java online submissions for Binary Tree Inorder Traversal.
Memory Usage: 36 MB, less than 90.56% of Java online submissions for Binary Tree Inorder Traversal.

Published 143 original articles · won praise 45 · views 70000 +

Guess you like

Origin blog.csdn.net/z714405489/article/details/89600987
Recommended