二叉树的建立,非递归中序,后序,先序

import java.util.LinkedList;
import java.util.Scanner;

class TreeNode{
    char val;
    TreeNode leftchild;
    TreeNode rightchild;
    int flag;//用于后续非递归
}
public class Tree {
    Scanner scanner= new Scanner(System.in);
    public TreeNode buildNode()
    {
        char s=scanner.next().charAt(0);
        if(s=='#') return null;
        TreeNode node= new TreeNode();
        node.val=s;
        node.leftchild=buildNode();
        node.rightchild=buildNode();
        return node;
    }
    public TreeNode buildTree()
    {
        TreeNode root=new TreeNode();
        root.val=scanner.next().charAt(0);
        root.leftchild=buildNode();
        root.rightchild=buildNode();
        return root;
    }
    public void output(TreeNode root)
    {
        if(root==null)return;
        System.out.print(root.val+" ");
        output(root.leftchild);
        output(root.rightchild);
    }
/**
 * ///测试数据
a
b
d
#
#
e
#
#
c
f
#
#
#
 **///中序
    public void NoRecurInnerOder(TreeNode root)
    {
        LinkedList<TreeNode> stack= new LinkedList<>();
        TreeNode cur=root;
        while (cur!=null||!stack.isEmpty())
        {
            while (cur!=null)
            {
                stack.push(cur);
                cur=cur.leftchild;
            }
            if(stack.isEmpty())break;
            cur=stack.pop();
            System.out.print(cur.val+" ");
            cur=cur.rightchild;
        }
    }//先序
    public void NoRecurPreOrder(TreeNode root)
    {
        LinkedList<TreeNode> stack= new LinkedList<>();
        TreeNode cur=root;
        cur=root;
        while (cur!=null||!stack.isEmpty())
        {
            while (cur!=null)
            {
                System.out.print(cur.val+" ");
                stack.push(cur);
                cur=cur.leftchild;
            }
            if(stack.isEmpty())break;
            cur=stack.pop();
            cur=cur.rightchild;
        }
    }//后序
    public void NoRecurPostOrder(TreeNode root)
    {
        TreeNode cur;
        LinkedList<TreeNode> stack= new LinkedList<>();
        cur=root;
        while (cur!=null||!stack.isEmpty())
        {
            while (cur!=null)
            {
                stack.push(cur);
                cur=cur.leftchild;
            }
            if(stack.peek().flag==1)
            {
                System.out.print(stack.pop().val+" ");
            }else {
                if (stack.isEmpty()) break;
                stack.peek().flag = 1;
                cur = stack.peek().rightchild;
            }
        }
    }
    public static void main(String[] args) {
        Tree tree=new Tree();
        TreeNode root= tree.buildTree();
        tree.NoRecurPostOrder(root);
    }
}

后序的第二种写法

 public ArrayList<Integer> postOrderTraversal(TreeNode root)
    {
        ArrayList<Integer> ans=new ArrayList<>();
        if(root==null)
            return ans;
        LinkedList<TreeNode> stack=new LinkedList<>();
        LinkedList<TreeNode> temp=new LinkedList<>();
        TreeNode cur=root;
        while (cur!=null||!stack.isEmpty())
        {
            if (cur!=null)
            {
                stack.push(cur);
                temp.push(cur);
                cur=cur.right;
            }else
            {
                if(temp.isEmpty())break;
                TreeNode node=temp.pop();
                cur=node.left;
            }
        }
        while (!stack.isEmpty())
        {
            ans.add(stack.pop().val);
        }
        return ans;
    }
posted @ 2021-05-18 21:58  LiangLiangAA  阅读(42)  评论(0编辑  收藏  举报
theme: { name: 'geek', avatar: '', headerBackground: '' // ... },