/*// 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;}intmx=0;for(Nodechild:root.children){mx=Math.max(mx,maxDepth(child));}return1+mx;}}
/*// 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;}intmx=0;for(Node*child:root->children){mx=max(mx,maxDepth(child));}returnmx+1;}};
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}mx:=0for_,child:=rangeroot.Children{mx=max(mx,maxDepth(child))}return1+mx}