zoukankan      html  css  js  c++  java
  • 二叉树遍历的非递归

    前序遍历的非递归:1、在入栈时增加结果集,不停的取左子树入栈。直到为空。2、假设栈非空,pop栈顶结点。取其右子树作为当前结点,继续第一步。直到栈为空

    中序遍历的非递归:1、在入栈时,不停的取左子树入栈,直到为空。2、假设栈非空,pop栈顶结点,增加结点集,取其右子树作为当前结点。继续第一步。直到栈为空

    后序遍历的非递归:1、在遍历结点时,总是先将右子树结点入栈,再将左子树结点入栈。

    2、假设左子树结点和右子树结点为空或者右子树结点已经訪问过,弹出栈顶元素,标记已訪问结点。增加结果集。直到栈为空。

    详细代码例如以下:

    class TreeNode
    {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
    
    class Solution
    {
        public List<Integer> preorderTraversal(TreeNode root)
        {
            List<Integer> ret = new ArrayList<Integer>();
    
            Stack<TreeNode> stack = new Stack<TreeNode>();
            TreeNode cur = root;
    
            while(cur != null || !stack.empty())
            {
                while (cur != null)
                {
                    ret.add(cur.val);
                    stack.push(cur);
                    cur = cur.left;
                }
    
                if (!stack.empty())
                {
                    TreeNode tmp = stack.pop();
                    cur = tmp.right;
                }
            }
    
            return ret;
        }
    
    
        public List<Integer> inorderTraversal(TreeNode root)
        {
            List<Integer> ret = new ArrayList<Integer>();
    
            Stack<TreeNode> stack = new Stack<TreeNode>();
            TreeNode cur = root;
    
            while(cur != null || !stack.empty())
            {
                while (cur != null)
                {
                    stack.push(cur);
                    cur = cur.left;
                }
    
                if (!stack.empty())
                {
                    TreeNode tmp = stack.pop();
                    ret.add(tmp.val);
                    cur = tmp.right;
                }
            }
    
            return ret;
        }
    
        public List<Integer> postorderTraversal(TreeNode root)
        {
            List<Integer> ret = new ArrayList<Integer>();
    
            Stack<TreeNode> stack = new Stack<TreeNode>();
            TreeNode cur = root, pre = null;
    
            if (null == cur) return null;
    
            stack.push(cur);
            while (!stack.empty())
            {
                TreeNode tmp = stack.peek();
    
                if ((tmp.left == null && tmp.right == null) || (pre != null && tmp.right == pre))
                {
                    ret.add(tmp.val);
                    stack.pop();
                    pre = tmp;
                }
                else
                {
                    if (tmp.right != null) stack.push(tmp.right);
                    if (tmp.left != null) stack.push(tmp.left);
                }
            }
    
            return ret;
        }
    }


  • 相关阅读:
    C/C++解题常用STL大礼包 含vector,map,set,queue(含优先队列) ,stack的常用用法
    PAT甲级1018留坑——第一个测试点未过(Dijikstar+Dfs)
    PAT甲级1019水题飘过
    微信该公众号提供的服务出现故障
    The valid characters are defined in RFC 7230 and RFC 3986
    eclipse中Web Deployment Assembly与build path作用
    Invalid bound statement (not found)
    springmvc获取bean
    mac/linux查询网络端口占用
    SiteMesh使用(2.4.2)
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7398779.html
Copyright © 2011-2022 走看看