二叉樹遞歸非遞歸遍歷算法整理

一、二叉樹前序遍歷

  • 1 前序遞歸遍歷
    public void preOrder(BinaryNode root) {
        if (root != null) {
            System.out.print(root.data + " ");
            preOrder(root.left);
            preOrder(root.right);
        }
    }
  • 2.前序非遞歸遍歷
    public void preOrderTraverse(BinaryNode root) {
        Stack<BinaryNode> stack = new Stack<>();
        BinaryNode node = root;
        while (node != null || !stack.empty()) {
            while (node != null) {
                stack.push(node);
                System.out.print(node.data + " ");
                node = node.left;
            }
            if (!stack.empty()) {
                node = stack.pop();
                node = node.right;
            }
        }
    }

一、二叉樹中序遍歷

  • 2.中序遞歸遍歷
    public void inOrder(BinaryNode root) {
        if (root != null) {
            inOrder(root.left);
            System.out.print(root.data + " ");
            inOrder(root.right);
        }
    }
  • 1.中序非遞歸遍歷
    public void inOrderTraverse(BinaryNode root) {
        Stack<BinaryNode> stack = new Stack<>();
        BinaryNode node = root;
        while (node != null || !stack.empty()) {
            while (node != null) {
                stack.push(node);
                node = node.left;
            }
            if (!stack.empty()) {
                node = stack.pop();
                System.out.print(node.data + " ");
                node = node.right;
            }
        }
    }

一、二叉樹后序遍歷

  • 1.后序遞歸遍歷
    public void postOrder(BinaryNode root) {
        if (root != null) {
            postOrder(root.left);
            postOrder(root.right);
            System.out.print(root.data + " ");
        }
    }
  • 2.后序非遞歸遍歷
    public void postOrderTraverse(BinaryNode root) {
        Stack<BinaryNode> stack1 = new Stack<>();
        Stack<BinaryNode> stack2 = new Stack<>();
        stack1.push(root);
        while (!stack1.empty()) {
            BinaryNode node = stack1.pop();
            stack2.push(node);
            if (node.left != null) {
                stack1.push(node.left);
            }
            if (node.right != null) {
                stack1.push(node.right);
            }
        }
        while (!stack2.empty()) {
            BinaryNode node = stack2.pop();
            System.out.print(node.data + " ");
        }
    }

四、二叉樹層次遍歷

    public void level(BinaryNode root) {
        ConcurrentLinkedQueue<BinaryNode> queue = new ConcurrentLinkedQueue<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            BinaryNode node = queue.peek();
            queue.remove();
            System.out.print(node.data + " ");
            if (node.left != null) {
                queue.add(node.left);
            }
            if (node.right != null) {
                queue.add(node.right);
            }
        }
    }
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容