题意
给一个二叉树
求深度
思路
1、空节点返回层数为0;
2、返回本层深度:非空节点深度=左右节点中最大层数+1;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public int TreeDepth(TreeNode root) {
if(root==null)return 0;
return Math.max(TreeDepth(root.left),TreeDepth(root.right))+1;
}
}