遍历二叉树非递归实现

实现

1. 前序遍历

public void preOrderNor(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode cur = stack.pop();
            System.out.print(cur.val + " ");
            if (cur.right != null) {
                stack.push(cur.right);
            }
            if (cur.left != null) {
                stack.push(cur.left);
            }
        }
    }

力扣测试

class Solution {
        public List<Integer> preorderTraversal(TreeNode root) {
            List<Integer> ret = new ArrayList<>();
            if (root == null) {
                return ret;
            }
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);
            while (!stack.isEmpty()) {
                TreeNode cur = stack.pop();
                ret.add(cur.val);
                if (cur.right != null) {
                    stack.push(cur.right);
                }
                if (cur.left != null) {
                    stack.push(cur.left);
                }
            }
            return ret;
        }
    }

 

2. 中序遍历

public void inOrderNor(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        while (root != null || !stack.isEmpty()) {
            if (root != null) {
                stack.push(root);
                root = root.left;
            }else {
                root = stack.pop();
                System.out.println(root.val + " ");
                root = root.right;
            }
        }
    }

力扣测试

class Solution {
        public List<Integer> inorderTraversal(TreeNode root) {

            List<Integer> ret = new ArrayList<>();
            if (root == null) {
                return ret;
            }

            Stack<TreeNode> stack = new Stack<>();
            while (root != null || !stack.isEmpty()) {
                if (root != null) {
                    stack.push(root);
                    root = root.left;
                }else {
                    root = stack.pop();
                    ret.add(root.val);
                    root = root.right;
                }
            }
            return ret;
        }
}

3. 后序遍历

两个栈实现:

public  void posOrderTwoStacks(TreeNode root) {
        if (root != null) {
            Stack<TreeNode> stack = new Stack<>();
            Stack<TreeNode> collect = new Stack<>();
            stack.push(root);
            while (!stack.isEmpty()) {
                root = stack.pop();
                collect.push(root);
                if (root.left != null) {
                    stack.push(root.left);
                }
                if (root.right != null) {
                    stack.push(root.right);
                }
            }
            while (!collect.isEmpty()) {
                System.out.print(collect.pop().val + " ");
            }
        }
    }

 

一个栈实现:

public  void posOrderOneStacks(TreeNode root) {
        if (root != null) {
            // 标记上一次访问的节点
            TreeNode sign = root;
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);
            while (!stack.isEmpty()) {
                TreeNode cur = stack.peek();
                // 如果sign标志右树 那么左树一定处理了
                if (cur.left != null && cur.left != sign && cur.right != sign) {
                    // 有左树且左树没有处理进来
                    stack.push(cur.left);
                }else if (cur.right != null && cur.right != sign) {
                    // 有右树且右树没有处理进来
                    stack.push(cur.right);
                }else {
                    System.out.print(cur.val + " ");
                    sign = stack.pop();
                }
            }
        }
    }
posted @ 2024-01-22 14:50  qyx1  阅读(24)  评论(0)    收藏  举报