1. 程式人生 > 其它 >【LeetCode】棧、佇列問題:用棧代替遞迴

【LeetCode】棧、佇列問題:用棧代替遞迴

技術標籤:# LeetCodeleetcode演算法

模擬遞迴

144. 二叉樹的前序遍歷

難度中等508收藏分享切換為英文接收動態反饋

給你二叉樹的根節點 root ,返回它節點值的 前序 遍歷。

示例 1:

在這裡插入圖片描述

輸入:root = [1,null,2,3]
輸出:[1,2,3]

示例 2:

輸入:root = []
輸出:[]

示例 3:

輸入:root = [1]
輸出:[1]

遞迴解法

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<
Integer>
list = new ArrayList<>(); search(root , list); return list; } void search(TreeNode root , List<Integer> list){ if(root != null){ list.add(root.val); search(root.left , list); search(root.right ,list); }
} }

棧模擬解法

  • 通用解法(適用於前、中、後,因為教材傳統的遍歷解法,只能應對前序遍歷)
  • 核心思想:因為自己模擬系統棧,無法做到返回到棧幀儲存的執行位置,所以說那麼可以把後續要執行的操作包裝起來也壓棧,即搞一個命令棧,命令中有不同欄位區分不同操作
class Solution {
    
    class Command{
        boolean isPrint;
        TreeNode node;

        Command(boolean isPrint , TreeNode node){
            this.isPrint =
isPrint; this.node = node; }; } public List<Integer> preorderTraversal(TreeNode root) { List<Integer> res = new LinkedList<>(); if(root == null){ return res; } LinkedList<Command> stack = new LinkedList<>(); stack.push(new Command(false , root)); while(stack.size() != 0){ Command cmd = stack.pop(); if(cmd.isPrint){ res.add(cmd.node.val); }else { if(cmd.node.right != null){ stack.push(new Command(false , cmd.node.right)); } if(cmd.node.left != null){ stack.push(new Command(false , cmd.node.left)); } stack.push(new Command(true , cmd.node)); } } return res; } }

94. 二叉樹的中序遍歷

難度中等851收藏分享切換為英文接收動態反饋

給定一個二叉樹的根節點 root ,返回它的 中序 遍歷。

示例 1:

在這裡插入圖片描述

輸入:root = [1,null,2,3]
輸出:[1,3,2]

示例 2:

輸入:root = []
輸出:[]

示例 3:

輸入:root = [1]
輸出:[1]

遞迴

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new LinkedList<>();
        func(root , res);
        return res;
    }

    void func(TreeNode root , List<Integer> res){
        if(root != null){
            func(root.left , res);
            res.add(root.val);
            func(root.right ,res);
        }
    }
}

迴圈

class Solution {
    class Command{
        boolean isPrint;
        TreeNode node;
        Command(boolean isPrint , TreeNode node){
            this.isPrint = isPrint;
            this.node = node;
        };
    }
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new LinkedList<>();
        if(root == null){
            return res;
        }

        LinkedList<Command> stack = new LinkedList<>();
        stack.push(new Command(false , root));

        while(stack.size() != 0){
            Command cmd =stack.pop();
            if(cmd.isPrint){
                res.add(cmd.node.val);
            }else{
                if(cmd.node.right != null){
                    stack.push(new Command(false , cmd.node.right));
                }
                stack.push(new Command(true , cmd.node)); // 中序
                if(cmd.node.left != null){
                    stack.push(new Command(false , cmd.node.left));
                }
            }
        }

        return res;
    }
}