二叉树中序遍历

来源:互联网 发布:淘宝买的酒是真的吗 编辑:程序博客网 时间:2024/06/10 16:43

递归

publicvoid MidOrder(BinaryTree head){
        if(head==null){
            return;
        }
       MidOrder(head.left);
        System.out.println(head.value);
        preOrder(head.right);
}
非递归
classBinaryTree{
    BinaryTreeleft;
    BinaryTreeright;
    intvalue;
    publicBinaryTree(intvalue){
        this.value= value;
    }
}
publicclassSolution {
    publicvoid MidOrder(BinaryTree head){
        if(head==null){
            return;
        }
       MidOrder(head.left);
        System.out.println(head.value);
        preOrder(head.right);
    }
    publicstaticvoidmain(String[]args){
        BinaryTree root =newBinaryTree(22);
        BinaryTree left =newBinaryTree(33);
        BinaryTree right =newBinaryTree(44);
        root.left= left;
        root.right= right;
        newSolution().preOrder(root);
    }
}




原创粉丝点击