Created
November 8, 2020 10:31
-
-
Save jacygao/9f2711cd62cb0a5326cbd2c59b350e1b to your computer and use it in GitHub Desktop.
Given a n-ary tree, find its maximum depth.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int maxDepth(Node root) { | |
if (root == null) { | |
return 0; | |
} | |
return dfs(root); | |
} | |
public int dfs(Node n) { | |
int maxChild = 0; | |
for (Node child : n.children) { | |
int childDep = dfs(child); | |
if (childDep > maxChild) { | |
maxChild = childDep; | |
} | |
} | |
maxChild++; | |
return maxChild; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment