二叉樹的幾種遍歷方法,包括遞歸和迭代

由於要找工作了複習下二叉樹。
public class BinaryTreeTraversal
{

public class TreeNode 
{
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
}

/*
 * 二叉樹三種遍歷的方法:
 * 遞歸前序遍歷
 */
public void  pre(TreeNode root)
{
    if(root==null)
        return ;
    System.out.println(root.val);
    pre(root.left);
    pre(root.right);
}
/*
 * 二叉樹三種遍歷的方法:
 * 遞歸中序遍歷
 */
public void  mid(TreeNode root)
{
    if(root==null)
        return ;
    mid(root.left);
    System.out.println(root.val);
    mid(root.right);
}
    /*
     * 二叉樹三種遍歷的方法:
     * 遞歸後續遍歷
     */
public void  pos(TreeNode root)
{
    if(root==null)
        return ;
    pos(root.left);
    pos(root.right);
    System.out.println(root.val);
}

/*
 * 二叉樹三種遍歷的方法:
 * 迭代前序遍歷
 */
public void itaPre(TreeNode root)
{
    Stack<TreeNode> stack=new Stack<TreeNode>();
    TreeNode cur=root;
    while(!stack.isEmpty()||cur!=null)
    {
        if(cur!=null)
        {
            System.out.println(cur.val);
            stack.push(cur);
            cur=cur.left;
        }
        else
        {
            cur=stack.pop();
            cur=cur.right;
        }
    }
}
/*
 * 二叉樹三種遍歷的方法:
 * 迭代中序遍歷
 */
public void itaMid(TreeNode root)
{
    Stack<TreeNode> stack=new Stack<TreeNode>();
    TreeNode cur=root;
    while(!stack.isEmpty()||cur!=null)
    {
        if(cur!=null)
        {
            stack.push(cur);
            cur=cur.left;
        }
        else
        {
            cur=stack.pop();
            System.out.println(cur.val);
            cur=cur.right;
        }
    }
}



/*
 * 二叉樹三種遍歷的方法:
 * 迭代後序遍歷
 */
public void itaPos(TreeNode root)
{
    Stack<TreeNode> stack=new Stack<TreeNode>();
    TreeNode cur=root;
    TreeNode pre=null;
    while(!stack.isEmpty()||cur!=null)
    {
        while(cur!=null)
        {
            stack.push(cur);
            cur=cur.left;
        }
        cur=stack.peek();
        if(cur.right==null||cur.right==pre)
        {
            System.out.println(cur.val);
            pre=cur;
            stack.pop();
            cur=null;
        }else
        {
            cur=cur.right;
        }
    }
}


/*
 * 二叉樹三種遍歷的方法:
 * 層次遍歷
 * 思路:首先將root壓入隊列中,如果隊列不爲null那麼出隊列輸出,同時判斷cur左右結點是否爲null不爲空進隊列;
 * 循環,那麼下一個出隊列的講是cur壓入隊列的那個值:
 */
public void  Level(TreeNode root)
{
    Queue<TreeNode> queue=new LinkedList<TreeNode>();
    TreeNode cur=null;
    queue.add(root);
    while(!queue.isEmpty())
    {
       cur=queue.poll();
       System.out.println(cur.val);
       if(cur!=null)
       {
           if(cur.left!=null)
               queue.add(cur.left);
           if(cur.right!=null)
               queue.add(cur.right);
       }
    }
}

@Test
public void test()
{
    TreeNode root=new TreeNode(0);
    TreeNode n1=new TreeNode(1);
    TreeNode n2=new TreeNode(2);
    TreeNode n3=new TreeNode(3);
    TreeNode n4=new TreeNode(4);
    TreeNode n5=new TreeNode(5);
    TreeNode n6=new TreeNode(6);
    root.left=n1;
    root.right=n2;
    n1.left=n3;
    n1.right=n4;
    n2.left=n5;
    n2.right=n6;
    Level(root);
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章