zoukankan      html  css  js  c++  java
  • Leetcode 559 N叉树的最大深度

    Leetcode 559 N叉树的最大深度

    数据结构定义:

    给定一个 N 叉树,找到其最大深度。
    
    最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
    
    N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。
    示例 1:
        				  1
        				/ | 
                                   3  2  4
                                  / 
                                 5   6
    输入:root = [1,null,3,2,4,null,5,6]
    输出:3
        
    /*
    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 maxDepth(Node root) {
            if(root == null){
                return 0;
            }
            int[] depthes = new int[root.children.size()];
            for(int i = 0;i< root.children.size(); ++i){
                depthes[i] = maxDepth(root.children.get(i));
            }
            return depthes.length > 0 ? Arrays.stream(depthes).max().getAsInt() + 1 : 1;
        }
    }
    

    递归的简洁方式:

    class Solution {
        public int maxDepth(Node root) {
            if(root == null){
                return 0;
            }
            int depth = 1;
            for(Node children :root.children){
                depth =  Math.max(depth,maxDepth(children) + 1);
            }
            return depth;
        }
    }
    

    迭代遍历:

    class Solution {
        public int maxDepth(Node root) {
            Queue<Pair<Node,Integer>> stack = new LinkedList<>();
            if(root != null){
                stack.offer(new Pair(root,1));
            }
            int max = 0;
            while(!stack.isEmpty()){
                Pair<Node,Integer> node = stack.poll();
                int value = node.getValue();
                root =  node.getKey();
                if(root != null){
                    max = Math.max(max,value);
                    for(Node temp : root.children){
                        stack.offer(new Pair(temp,value + 1));
                     }
                }
            }
            return max;
        }
    }
    
  • 相关阅读:
    解决“Caused by: org.gradle.api.plugins.UnknownPluginException: Plugin with id 'org.springframework.boot' not found.”
    linux随机生成密码
    NFS网络共享文件系统
    shell实现带颜色输出的进度条
    【AtCoder】 ARC 101
    【AtCoder】 ARC 102
    【AtCoder】 ARC 103
    20190814校内模拟赛
    「2019-8-13提高模拟赛」树 (tree)
    [PA2014]Fiolki
  • 原文地址:https://www.cnblogs.com/CodingXu-jie/p/14101837.html
Copyright © 2011-2022 走看看