码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode——N叉树的遍历

时间:2020-05-07 15:52:30      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:tac   树的遍历   pre   tco   tor   poll   img   ack   linked   

Define:

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

    public Node() {
    }

    public Node(int _val) {
        val = _val;
    }

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

前序遍历

技术图片

同二叉树前序遍历。

递归

    private List<Integer> res;

    public List<Integer> preorder(Node root) {
        res = new LinkedList<>();
        pre(root);
        return res;
    }

    private void pre(Node root) {
        if (root == null)
            return;
        res.add(root.val);
        for (Node curr : root.children) {
            pre(curr);
        }
    }

非递归

    public List<Integer> preorder(Node root) {
        List<Integer> result = new LinkedList<>();
        if (root == null)
            return result;
        Stack<Node> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            Node temp = stack.pop();
            result.add(temp.val);
            for (int i = temp.children.size() - 1; i >= 0; i--) {
                Node curr = temp.children.get(i);
                stack.push(curr);
            }
        }
        return result;
    }

后序遍历

技术图片

同二叉树的后序遍历。

递归

    private List<Integer> res;
    public List<Integer> postorder(Node root) {
        res = new LinkedList<>();
        post(root);
        return res;
    }

    private void post(Node root) {
        if (root == null)
            return;
        for (Node curr : root.children) {
            post(curr);
        }
        res.add(root.val);
    }

非递归

    public List<Integer> postorder(Node root) {
        List<Integer> result = new LinkedList<>();
        if (root == null)
            return result;
        Stack<Node> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            Node temp = stack.pop();
            result.add(0, temp.val);//change1
            for (int i = 0; i < temp.children.size(); i++) {//change2
                Node curr = temp.children.get(i);
                stack.push(curr);
            }
        }
        return result;
    }

层次遍历

技术图片

同二叉树的层次遍历。

    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null)
            return res;
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        int count = 1;
        while (!queue.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            while (count-- != 0) {
                Node curr = queue.poll();
                list.add(curr.val);
                queue.addAll(curr.children);
            }
            count = queue.size();
            res.add(list);
        }
        return res;
    }

LeetCode——N叉树的遍历

标签:tac   树的遍历   pre   tco   tor   poll   img   ack   linked   

原文地址:https://www.cnblogs.com/xym4869/p/12841485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!