• 二叉树遍历的非递归


    前序遍历的非递归: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;
        }
    }


  • 相关阅读:
    滚动 冻结 div demo
    JavaScript去除字符串两边空格trim
    window.showModalDialog以及window.open用法简介
    转: 分享我创业4年失败的经历
    [转]JavaScript break跳出多重循环
    showModalDialog 传值及刷新
    防止文字撑开表格,强制表格大小
    带记忆功能的表单
    checkbox 全选
    asp教程:关于jquery跨域彻底的解决方法
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7398779.html
Copyright © 2020-2023  润新知