class Solution {
public int maxDepth(TreeNode root) {
return dfs(root);
}
private int dfs(TreeNode node){
if(node == null)return 0;
int leftHeight = dfs(node.left);
int rightHeight = dfs(node.right);
return 1 + Math.max(leftHeight, rightHeight);
}
}
class Solution {
public int maxDepth(Node root) {
int res = 0;
if(root == null)return res;
for(Node child : root.children){
res = Math.max(maxDepth(child), res);
}
return res + 1;
}
}
class Solution {
public int minDepth(TreeNode root) {
if(root == null)return 0;
int leftDepth = minDepth(root.left);
int rightDepth = minDepth(root.right);
if(root.left == null && root.right != null){
return 1 + rightDepth;
}
if(root.left != null && root.right == null){
return 1 + leftDepth;
}
int res = 1 + Math.min(leftDepth, rightDepth);
return res;
}
}
class Solution {
public int countNodes(TreeNode root) {
if(root == null)return 0;
int leftNum = countNodes(root.left);
int rightNum = countNodes(root.right);
return 1 + leftNum + rightNum;
}
}