Non-recursive way to realize the front, middle and post-order traversal of the binary tree

Dear friends, happy May Day everyone. What I will share with you today is to realize the pre-, middle-, and post-order traversal of a binary tree without recursion. Let's take a look.

Preorder traversal of a binary tree

Although we are talking about implementation without recursion, our idea is to simulate the implementation of recursion, so how do we simulate recursion without recursion? If we want to simulate recursion, we need to know what is the underlying logic to achieve recursion? Recursion is actually implemented through the data structure of the stack. We all know that the order of recursion is completed first and then recursive. This is similar to the principle of stack first-in-last-out. Therefore, if we want to simulate recursion, we also need to borrow the stack this data structure.

After knowing the principle, we need to know how to implement it next, because here is the preorder traversal, first traverse the root node, then the left subtree, and finally the right subtree, so we traverse the left subtree first, And while traversing, we store the traversed nodes in the stack and print them, so that we can find the right subtree of the node. When our root traverses to null, it means that the left tree has been traversed, and we pop the node on the top of the stack, and make root equal to the right subtree of the popped node, until the root is equal to null and the stack is empty to complete the traverse.

insert image description here

insert image description here
insert image description here
The right tree of node 6 is also null, so we pop the top element of the stack, cur = top.right, and continue the above operation.
insert image description here
insert image description here

insert image description here

insert image description here

public void preOrderNor(TreeNode root) {
    
    
        if(root == null) {
    
    
            return;
        }
        TreeNode cur = root;
        Deque<TreeNode> stack = new ArrayDeque<>();
        while(cur != null || !stack.isEmpty()) {
    
    
            while(cur != null) {
    
    
                stack.push(cur);
                System.out.println(cur.val);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }

Inorder traversal of a binary tree

The idea of ​​in-order traversal is the same as that of pre-order traversal, but the order of printing is different. We print while popping nodes from the stack.

public void inOrderNor(TreeNode root) {
    
    
        if(root == null) {
    
    
            return;
        }
        Deque<TreeNode> stack = new ArrayDeque<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()) {
    
    
            while(cur != null) {
    
    
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.println(top.val);
            cur = top.right;
        }
    }

Post-order traversal of a binary tree

The idea of ​​post-order traversal is different from that of pre-order and in-order. We need to judge when to print. Because it is post-order traversal, we need to traverse the root node after traversing the right tree or when the right tree is null.
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

public void postOrderNor(TreeNode root) {
    
    
        if(root == null) {
    
    
            return;
        }
        TreeNode cur = root;
        TreeNode prev = null;
        Deque<TreeNode> stack = new ArrayDeque<>();
        while(cur != null || !stack.isEmpty()) {
    
    
            while(cur != null) {
    
    
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.peek();
            if(top.right == null || top.right == prev) {
    
    
                System.out.println(top.val);
                stack.pop();
                prev = top;
            }else {
    
    
                cur = top.right;
            }
        }
    }

Guess you like

Origin blog.csdn.net/m0_73888323/article/details/130473089