559_N叉树的最大深度

559_N叉树的最大深度

package 二叉树.BT;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
 * https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/
 * 
 * @author Huangyujun
 *
 */
public class _559_N叉树的最大深度 {
    public 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;
        }
    }


 //这样是不对的(原因想不明白):官网是把每个孩子结点的maxDepth 存储起来,最后才去拿那个最大值
    //正解:遍历每个孩子前  height 必须等于  1;
    int height = 0;
    public int maxDepth(Node root) {
        if (root == null) {
            return 0;
        } else if (root.children.isEmpty()) {
            return 1;
        } else {
            height = 1;
            for (Node node : root.children) {    //知道原因了孩子是同一级的:1 + maxDepth(node)
                height = Math.max(height, 1 + maxDepth(node));
                
            }
        }
        return height;
    }

    class Solution {
          public int maxDepth(Node root) {
            if (root == null) {
              return 0;
            } else if (root.children.isEmpty()) {
              return 1;  
            } else {
              List<Integer> heights = new LinkedList<>();
              for (Node item : root.children) {
                heights.add(maxDepth(item)); 
              }
              return Collections.max(heights) + 1;
            }
          }
        }

    //迭代法
    public int maxDepth2(Node root) {
        if(root == null) return 0;
        int height = 0;
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        int levelSize = 1;
        while(!queue.isEmpty()) {
            Node node = queue.poll();
            levelSize--;
            for(int i = 0; i < node.children.size(); i++) {
                if(node.children.get(i) != null)
                queue.add(node.children.get(i));
            }
            if(levelSize == 0){
                height++;
                levelSize = queue.size();
            }
        }

        return height; 
    }
// 小知识:
//调用poll()和pop()都可以返回队首元素并将其从原队列删除。不同的是当队列为空时,调用pop()会抛出异常,而poll()会返回null
    
    //思路:通过一个类似map 键值对的集合,然后,将(结点   深度)的添加进去,然后又通过 
    //栈 (这不是一个纯栈):不断的:(poll 出来当前结点,然后深度 + 1, add 进去 子结点)
    //poll() 出来,后得到当前结点,将当前结点的深度加 1,然后把当前结点的所有 子结点 添加进栈
    
    
//     public int maxDepth(Node root) {
//         //map的话,无法直接通过 当前取出来一对键值对,然后直接获取其键 或 值
//            Queue<Pair<Node, Integer>> stack = new LinkedList<>();
//            if (root != null) {
//              stack.add(new Pair(root, 1));
//            }
//
//            int depth = 0;
//            while (!stack.isEmpty()) {
//              Pair<Node, Integer> current = stack.poll();
//              root = current.getKey();
//              int current_depth = current.getValue();
//              if (root != null) {
//                depth = Math.max(depth, current_depth);
//                for (Node c : root.children) {
//                  stack.add(new Pair(c, current_depth + 1));    
//                }
//              }
//            }
//            return depth;
//          }


}

本文来自博客园,作者:一乐乐,转载请注明原文链接:https://www.cnblogs.com/shan333/p/15709252.html

原文地址:https://www.cnblogs.com/shan333/p/15709252.html