2019.3.36

2019.3.36

//exercise 25.3
    public void inorderUsingStack(){
        inorderUsingStack(root);
    }

    private void inorderUsingStack(TreeNode<T> root){
        if(root == null)
            return;
        Stack<TreeNode<T>> stack = new Stack<>();
        TreeNode<T> current = root;
        while(current != null || !stack.isEmpty()){
            if(current != null){
                stack.push(current);
                current = current.left;
            }
            else{
                current = stack.pop();
                System.out.print(current.element+" ");
                current = current.right;
            }
        }
    }

在這裏插入圖片描述

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