/*// 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; }};*/classSolution{publicintmaxDepth(Noderoot){if(root==null){return0;}intans=1;for(Nodechild:root.children){ans=Math.max(ans,1+maxDepth(child));}returnans;}}
/*// Definition for a Node.class Node {public: int val; vector<Node*> children; Node() {} Node(int _val) { val = _val; } Node(int _val, vector<Node*> _children) { val = _val; children = _children; }};*/classSolution{public:intmaxDepth(Node*root){if(!root)return0;intans=1;for(auto&child:root->children)ans=max(ans,1+maxDepth(child));returnans;}};
1 2 3 4 5 6 7 8 9101112131415161718
/** * Definition for a Node. * type Node struct { * Val int * Children []*Node * } */funcmaxDepth(root*Node)int{ifroot==nil{return0}ans:=1for_,child:=rangeroot.Children{ans=max(ans,1+maxDepth(child))}returnans}