Three traversals of LeetCode 144 145 94 binary tree -- iterative method

Posted by amrigo on Tue, 01 Feb 2022 05:47:06 +0100

Reference article 1
Reference article 2

Idea:

Previously, we learned to use recursive method to traverse three kinds of binary trees. This time, we consider using iterative method to traverse and stack. It is recommended to read both reference articles. Among the two reference articles, the first one talks about how to use the iterative method to traverse, but the implementation of the three traversal methods is not particularly unified. It is impossible to simply change from one traversal order to another by modifying the access time of the intermediate elements like the recursive method, but the code needs to be greatly modified. The second part talks about how to transform this iterative method into a unified way.

The code attached in this article uses the unified implementation method mentioned in the second reference article.

Note:

1. The order of pushing the stack needs attention. No matter what kind of traversal order, the left son always traverses before the right son. However, when pushing them into the stack, pay attention to pushing the right son into the stack first, and then pushing the left son into the stack, so that the order is correct when they are popped out of the stack for access. Refer to Article 1 for details.

2. Pushing a null node into the stack plays a role of marking. Whenever a null node is encountered, it means that we have pushed the left and right sons of the next node of the null node into the stack. This time, we can directly output the value of the next node, which is described in detail in reference article 2.

Java code:

LeetCode 144 preorder traversal of binary trees

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if (root == null) return ans;
        Deque<TreeNode> deque = new LinkedList<>();
        deque.push(root);
        while (!deque.isEmpty()) {
            TreeNode rnNode = deque.pop();
            if (rnNode == null) {
                rnNode = deque.pop();
                ans.add(rnNode.val);
            } else {
                if (rnNode.right != null) deque.push(rnNode.right);
                if (rnNode.left != null) deque.push(rnNode.left);
                deque.push(rnNode);
                deque.push(null);
            }
        }
        return ans;
    }
}

Post order traversal of LeetCode 145 binary tree

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if (root == null) return ans;
        Deque<TreeNode> deque = new LinkedList<>();
        deque.push(root);
        while (!deque.isEmpty()) {
            TreeNode rnNode = deque.pop();
            if (rnNode == null) {
                rnNode = deque.pop();
                ans.add(rnNode.val);
            } else {
                deque.push(rnNode);
                deque.push(null);
                if (rnNode.right != null) deque.push(rnNode.right);
                if (rnNode.left != null) deque.push(rnNode.left);
            }
        }
        return ans;
    }
}

LeetCode 94 middle order traversal of binary tree

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if (root == null) return ans;
        Deque<TreeNode> deque = new LinkedList<>();
        deque.push(root);
        while (!deque.isEmpty()) {
            TreeNode rnNode = deque.pop();
            if (rnNode == null) {
                rnNode = deque.pop();
                ans.add(rnNode.val);
            } else {
                if (rnNode.right != null) deque.push(rnNode.right);
                deque.push(rnNode);
                deque.push(null);
                if (rnNode.left != null) deque.push(rnNode.left);
            }
        }
        return ans;
    }
}

Topics: Algorithm data structure leetcode