二叉树遍历

二叉树的递归遍历算法就不用说了;在非递归算法中,后序遍历难度大,需要标记当前是从哪个子树遍历回来。


public class Binary_Tree_Postorder_Traversal {



    /* 递归 */
    public static List<Integer> mypostorderTraversal(TreeNode root) {

        List<Integer> list = new ArrayList<>();
        if (root == null)
            return list;

        DFS(root,list);

        return list;
    }

    private static void DFS(TreeNode root,List list) {

        if (root.left != null)
            DFS(root.left,list);

        if (root.right != null)
            DFS(root.right,list);

        list.add(root.val);
        return;
    }


    /*后序非递归,利用栈模拟
    * 需要辅助标签,来表示当前节点是从左右哪边返回,即已遍历左子树或右子树,0表示从左子树返回*/

    public static List<Integer> postorderTraversal(TreeNode root) {

        List<Integer> list = new ArrayList<>();
        if (root == null)
            return list;

        Stack<TreeNode> stack = new Stack<>();


        int[] tag = new int[100];
        //标签,用来标记该节点左右子树的遍历情况

        TreeNode tmp = root;

        while (tmp != null || !stack.isEmpty())
        {
            while (tmp!=null)
            {
                stack.push(tmp);
                tag[stack.size()] = 0;
                tmp = tmp.left;
            }
            while (!stack.isEmpty()&&tag[stack.size()]==1)
            {
                list.add(stack.pop().val);                  
                //最后从右字数回来 , 已经是后序了
            }

            if (!stack.isEmpty())
            {
                tag[stack.size()] = 1;
                tmp = stack.peek();
                tmp = tmp.right;
            }

        }


        return list;
    }


    /* 先序遍历 非递归*/
    public static List<Integer> preorderTraversal(TreeNode root) {

        List<Integer> list = new ArrayList<>();
        if (root == null)
            return list;

        Stack<TreeNode> stack = new Stack<>();
        TreeNode tmp = root;


        while (tmp!=null || !stack.isEmpty())
        {
            while (tmp!=null)
            {
                list.add(tmp.val);
                stack.push(tmp);
                tmp = tmp.left;
            }

            if (!stack.isEmpty())
            {
                tmp = stack.pop();
                tmp = tmp.right;
            }
        }

        return list;
    }


    /* 中序遍历 非递归*/
    public static List<Integer> inorderTraversal(TreeNode root) {

        List<Integer> list = new ArrayList<>();
        if (root == null)
            return list;

        Stack<TreeNode> stack = new Stack<>();
        TreeNode tmp = root;


        while (tmp!=null || !stack.isEmpty())
        {
            while (tmp!=null)
            {
                stack.push(tmp);
                tmp = tmp.left;
            }

            if (!stack.isEmpty())
            {
                tmp = stack.pop();
                list.add(tmp.val);
                tmp = tmp.right;
            }
        }

        return list;
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值