LeetCode 589. N叉树的前序遍历
给定一个 N 叉树,返回其节点值的前序遍历。
例如,给定一个 3叉树
:
public int maxDepth(Node root) {
//1、如果根节点为空 返回0
if (root == null) {
return 0;
}
int maxChildHeight = 0;
//2、获取child节点
List<Node> children = root.children;
//3、如果child是empty,深度为1
if (children.isEmpty()) {
return 1;
} else {
//4、遍历子节点
for (int i = 0; i < children.size(); i++) {
maxChildHeight = Math.max(maxDepth(children.get(i)), maxChildHeight);
}
}
return maxChildHeight + 1;
}