(Java) Three kinds of traversal of binary tree (non-recursive implementation)

content

Non-recursive implementation of preorder traversal (LeetCode144)

Non-recursive implementation of in-order traversal (LeetCode94)

Non-recursive implementation of subsequent traversal (LeetCode145) 


Non-recursive implementation of preorder traversal (LeetCode144)

Note: The elements traversed below will be saved in the List and returned

The order of preorder traversal: root---left---right 

For the non-recursive implementation of the preorder traversal of the binary tree, the stack is a data structure. The specific methods are as follows:

1. Create a stack and save the root node of the binary tree in the stack

2. When the stack is not empty, remove the top element of the stack and traverse

3. If the root node has a right subtree, save the root node of the right subtree in the stack

4. Then traverse the left subtree of the root, and the order of traversing the left subtree is also to traverse the root first to see if there is a right subtree, then continue to traverse the left subtree, and loop in this order

5. Finally, when the stack is empty, return to List

The above process is illustrated with a diagram for easy understanding:

Reference Code:

public class Solution {
    public IList<int> PreorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> s = new Stack<>();
        if(root==null){
            return list;
        }
        TreeNode cur = root;
        s.push(cur);
        while(!s.empty()){
            cur = s.pop();
              while(cur!=null){
            list.add(cur.val);
            if(cur.right!=null){
                s.push(cur.right);
            }
            cur = cur.left;
        }
        }
        return list;
    }
}

Non-recursive implementation of in-order traversal (LeetCode94)

The order of in-order traversal: left---root---right , save the result of traversal in List

To achieve non-recursive traversal with the help of the stack data structure, the specific methods are as follows:

1. Push the root node and the root node of the left subtree into the stack in turn

2. Take out the top element of the stack and traverse

3. Access the right subtree of the top element of the stack

4. The right subtree of the top element of the stack is also a binary tree, so the above operations are looped

Drawing instructions:

Reference Code:

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> s = new Stack<>();
        if(root == null){
            return list;
        }
        TreeNode cur = root;
        while(!s.empty()||cur!=null){
            while(cur!=null){
                s.push(cur);
                cur = cur.left;
            }
            cur = s.pop();
            list.add(cur.val);
            cur = cur.right;
        }
        return list;
    }
}

Non-recursive implementation of subsequent traversal (LeetCode145) 

Subsequent traversal order: left---right---root , save the traversal result in List

To achieve non-recursive subsequent traversal, the stack is also a data structure. The specific methods are as follows:

1. The first step is the same as in-order traversal, and the root node of the left subtree is stored in the stack in turn.

2. Get the top element of the stack, if the right subtree of the node is empty, traverse the node, otherwise traverse the right subtree of the node

3. The right subtree is again a binary tree, continue to loop the above operations

Note: mark each traversed element with pre, here every time the top element of the stack is obtained, when the right of the top element of the stack is empty or equal to the marked node, the element is traversed at this time, the purpose of this is to prevent loops , resulting in a stack overflow.

Drawing instructions:

Reference Code:

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> s = new Stack<>();
        if(root==null){
            return list;
        }
        TreeNode cur = root;
        TreeNode pre = null;
        while(!s.empty()||cur!=null){
            while(cur!=null){
                s.push(cur);
                cur = cur.left;
            }
            cur = s.peek();
            if(cur.right==null||cur.right==pre){
                list.add(cur.val);
                s.pop();
                pre = cur;
                cur = null;
            }else{
                cur = cur.right;
            }
        }
        return list;
    }
}

Guess you like

Origin blog.csdn.net/qq_58710208/article/details/121414737