给定一个 N 叉树,返回其节点值的前序遍历

例如,给定一个 3叉树 :

LeetCode 589. N叉树的前序遍历

 

  public int maxDepth(Node root) {
        //1、如果根节点为空 返回0
        if (root == null) {
            return 0;
        }
        int maxChildHeight = 0;
        //2、获取child节点
        List<Node> children = root.children;
        //3、如果child是empty,深度为1
        if (children.isEmpty()) {
            return 1;
        } else {
            //4、遍历子节点
            for (int i = 0; i < children.size(); i++) {
                maxChildHeight = Math.max(maxDepth(children.get(i)), maxChildHeight);
            }
        }
        return maxChildHeight + 1;
    }

LeetCode 589. N叉树的前序遍历

相关文章:

  • 2021-10-15
  • 2022-01-09
  • 2022-02-10
  • 2021-09-22
  • 2021-07-08
  • 2022-02-03
  • 2022-12-23
  • 2021-09-11
猜你喜欢
  • 2021-12-23
  • 2021-05-06
  • 2021-07-30
  • 2022-01-11
  • 2021-11-24
  • 2021-06-17
相关资源
相似解决方案