欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

一篇文章教你如何用多种迭代写法实现二叉树遍历

程序员文章站 2022-06-17 22:49:54
目录思想利用栈和队列都可以实现树的迭代遍历。递归的写法将这个遍历的过程交给系统的堆栈去实现了,所以思想都是一样的、无非就是插入值的时机不一样。利用栈的先进先出的特点,对于前序遍历、我们可以先将当前的值...

思想

利用栈和队列都可以实现树的迭代遍历。递归的写法将这个遍历的过程交给系统的堆栈去实现了,所以思想都是一样的、无非就是插入值的时机不一样。利用栈的先进先出的特点,对于前序遍历、我们可以先将当前的值放进结果集中,表示的是根节点的值、然后将当前的节点加入到栈中、当前的节点等于自己的left、再次循环的时候、也会将left作为新的节点、直到节点为空、也就是走到了树的最左边、然后回退、也就是弹栈、、也可以认为回退的过程是从低向上的、具体就是让当前的节点等于栈弹出的right、继续重复上面的过程,也就实现了树的前序遍历、也就是bfs.后续遍历、中序遍历思想也是类似的。

实现

    public list<integer> preordertraversal1(treenode root) {
        list<integer> res = new arraylist<>();
        stack<treenode> stack = new stack<>();
        while (!stack.isempty() || root != null) {
            while (root != null) {
                res.add(root.val);
                stack.add(root);
                root = root.left;
            }
            treenode cur = stack.pop();
            root = cur.right;
        }
        return res;
    }
    public list<integer> preordertraversal2(treenode root) {
        list<integer> res = new arraylist<>();
        stack<treenode> stack = new stack<>();
        while (!stack.isempty() || root != null) {
            if (root != null) {
                res.add(root.val);
                stack.add(root);
                root = root.left;
            } else {
                treenode cur = stack.pop();
                root = cur.right;
            }
        }
        return res;
    }
    public list<integer> preordertraversal3(treenode root) {
        list<integer> res = new arraylist<>();
        if (root == null) return res;
        stack<treenode> stack = new stack<>();
        stack.push(root);
        while (!stack.isempty()) {
            treenode cur = stack.pop();
            res.add(cur.val);
            if (cur.right != null) {
                stack.push(cur.right);
            }
            if (cur.left != null) {
                stack.push(cur.left);
            }
        }
        return res;
    }
    public list<integer> preordertraversal4(treenode root) {
        list<integer> res = new arraylist<>();
        if (root == null) {
            return res;
        }
        linkedlist<treenode> queue = new linkedlist<>();
        queue.add(root);
        while (!queue.isempty()) {
            root = queue.poll();
            res.add(root.val);
            if (root.right != null) {
                queue.addfirst(root.right);
            }
            if (root.left != null) {
                root = root.left;
                while (root != null) {
                    res.add(root.val);
                    if (root.right != null) {
                        queue.addfirst(root.right);
                    }
                    root = root.left;
                }
            }
        }
        return res;
    }
    public list<integer> inordertraversal1(treenode root) {
        list<integer> res = new arraylist<>();
        stack<treenode> stack = new stack<>();
        while (root != null || !stack.isempty()) {
            if (root != null) {
                stack.add(root);
                root = root.left;
            } else {
                treenode cur = stack.pop();
                res.add(cur.val);
                root = cur.right;
            }
        }
        return res;
    }
    public list<integer> inordertraversal2(treenode root) {
        list<integer> res = new arraylist<>();
        stack<treenode> stack = new stack<>();
        while (root != null || !stack.isempty()) {
            while (root != null) {
                stack.add(root);
                root = root.left;
            }
            treenode cur = stack.pop();
            res.add(cur.val);
            root = cur.right;
        }
        return res;
    }
    public list<integer> postordertraversal1(treenode root) {
        list<integer> res = new arraylist<>();
        if (root == null) return res;
        stack<treenode> stack = new stack<>();
        stack.push(root);
        while (!stack.isempty()) {
            treenode cur = stack.pop();
            res.add(cur.val);
            if (cur.left != null) {
                stack.push(cur.left);
            }
            if (cur.right != null) {
                stack.push(cur.right);
            }
        }
        collections.reverse(res);
        return res;
    }
    public list<integer> postordertraversal2(treenode root) {
        list<integer> res = new arraylist<>();
        stack<treenode> stack = new stack<>();
        while (!stack.isempty()) {
            while (root != null) {
                res.add(root.val);
                stack.push(root);
                root = root.right;
            }
            treenode cur = stack.pop();
            root = cur.left;
        }
        collections.reverse(res);
        return res;
    }
    public list<list<integer>> levelorder(treenode root) {
        list<list<integer>> ret = new arraylist<>();
        if(root == null)return ret;
        queue<treenode> queue = new linkedlist<>();
        queue.offer(root);
        while (!queue.isempty()){
            int size = queue.size();
            list<integer> list = new arraylist<>();
            while(size!=0){
                treenode cur = queue.poll();
                list.add(cur.val);
                if(cur.left!=null){
                    queue.offer(cur.left);
                }
                if(cur.right!= null){
                    queue.offer(cur.right);
                }
                size --;
            }
            ret.add(list);
        }
        return ret;
    }

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注的更多内容!