天天看點

Leetcode N叉樹周遊方式總結1. 資料結構2. 前序周遊3. 後序周遊4. 層序周遊

文章目錄

  • 1. 資料結構
  • 2. 前序周遊
  • 3. 後序周遊
  • 4. 層序周遊

1. 資料結構

class Node {
    public int val;
    public List<Node> children;

    public Node() {
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
}
           

2. 前序周遊

Leetcode 589. N叉樹的前序周遊

遞歸實作:

class Solution {
    public List<Integer> preorder(Node root) {
        List<Integer> res = new ArrayList<>();
        helper(root, res);
        return res;
    }

    private void helper(Node root, List<Integer> res) {
        if (root == null) {
            return;
        }

        res.add(root.val);
        for (Node node : root.children) {
            helper(node, res);
        }
    }
}
           

疊代實作:

class Solution {
    public List<Integer> preorder(Node root) {
        if (root == null) {
            return new ArrayList<>();
        }

        List<Integer> res = new ArrayList<>();
        Stack<Node> stk = new Stack<>();
        stk.push(root);
        while (!stk.isEmpty()) {
            Node node = stk.pop();
            res.add(node.val);
            for (int i = node.children.size() - 1; i >= 0; i--) {
                stk.push(node.children.get(i));
            }
        }
        return res;
    }
}
           

3. 後序周遊

Leetcode 590. N叉樹的後序周遊

遞歸實作:

class Solution {
    public List<Integer> postorder(Node root) {
        List<Integer> res = new ArrayList<>();
        helper(root, res);
        return res;
    }

    private void helper(Node root, List<Integer> res) {
        if (root == null) {
            return;
        }

        for (Node node : root.children) {
            helper(node, res);
        }
        res.add(root.val);
    }
}
           

疊代實作:

class Solution {
    public List<Integer> postorder(Node root) {
        if (root == null) {
            return new ArrayList<>();
        }

        List<Integer> res = new ArrayList<>();
        Stack<Node> stk = new Stack<>();
        stk.push(root);
        while (!stk.isEmpty()) {
            Node node = stk.pop();
            res.add(node.val);
            for (int i = 0; i < node.children.size(); i++) {
                stk.push(node.children.get(i));
            }
        }
        Collections.reverse(res);
        return res;
    }
}
           

4. 層序周遊

Leetcode 429. N叉樹的層序周遊

遞歸實作:

class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> res = new ArrayList<>();
        helper(root, 0, res);
        return res;
    }

    private void helper(Node root, int level, List<List<Integer>> res) {
        if (root == null) {
            return;
        }
        if (level == res.size()) {
            res.add(new ArrayList<>());
        }
        res.get(level).add(root.val);
        for (Node node : root.children) {
            helper(node, level + 1, res);
        }
    }
}
           

疊代實作:

class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        if (root == null) {
            return new ArrayList<>();
        }

        List<List<Integer>> res = new ArrayList<>();
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int n = queue.size();
            List<Integer> ans = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                Node node = queue.remove();
                ans.add(node.val);
                for (int j = 0; j < node.children.size(); j++) {
                    queue.add(node.children.get(j));
                }
            }
            res.add(ans);
        }
        return res;
    }
}