LeetBook-N叉树

41 阅读2分钟

N叉树

N 叉树 - LeetBook - 力扣(LeetCode)全球极客挚爱的技术成长平台

N叉树的前序遍历

解法1

递归

/*
// Definition for a Node.
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;
    }
};
*/

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

    public void helper (Node root, List<Integer> result) {
        if (root == null) {
            return;
        }
        result.add (root.val);
        for (Node ch : root.children) {
            helper(ch, result);
        }
    }
}

N叉树的后序遍历

解法1

递归

/*
// Definition for a Node.
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;
    }
};
*/

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

    public void helper(Node root, List<Integer> result) {
        if (root == null) {
            return;
        }
        for (Node ch : root.children) {
            helper(ch, result);
        }
        result.add (root.val);
    }
}

N叉树的层序遍历

解法1

递归

/*
// Definition for a Node.
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;
    }
};
*/

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

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

N叉树的最大深度

解法1

  1. 复用上一题,层序遍历
  2. 增加一个max参数
  3. 将当前最大层数赋给max
  4. 返回max
/*
// Definition for a Node.
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;
    }
};
*/

class Solution {
    public int max = 0;
    public int maxDepth(Node root) {
        if (root == null) {
            return 0;
        }
        levelOrder(root);
        return max + 1;
    }
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> result = new ArrayList<>();
        helper(root, result, 0);
        return result;
    }

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

解法2

在解法1的基础上优化,去掉用不到的层序赋值

/*
// Definition for a Node.
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;
    }
};
*/

class Solution {
    public int max = 0;
    public int maxDepth(Node root) {
        if (root == null) {
            return 0;
        }
        levelOrder(root);
        return max + 1;
    }
    public void levelOrder(Node root) {
        helper(root, 0);
    }

    public void helper(Node root, int level) {
        if (root == null) {
            return;
        }
        if (level > max) {
            max = level;
        }
        for (Node ch : root.children) {
            helper(ch, level + 1);
        }
    }
}