104.二叉树的最大深度
leetcode:搜索 - 力扣(LeetCode)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}else{
int leftHight = maxDepth(root.left);
int rightHight = maxDepth(root.right);
return Math.max(leftHight,rightHight)+1;
}
}
}
559.n叉树的最大深度
leetcode:. - 力扣(LeetCode)
/*
// 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;
}
};
*/
class Solution {
public int maxDepth(Node root) {
if(root == null ){
return 0;
}
int maxChildDepth = 0;
List<Node> children = root.children;
for(Node child:children){
int childDepth = maxDepth(child);
maxChildDepth = Math.max(maxChildDepth,childDepth);
}
return maxChildDepth+1;
}
}