leetcode-104 二叉树的最大深度
随便一种遍历方式,遍历的同时记录深度,水题。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private int max = 0;
public void getDepth(TreeNode root, int d){
if (root == null){
return ;
}
if (d > max){
max = d;
}
getDepth(root.left, d+1);
getDepth(root.right, d + 1);
}
public int maxDepth(TreeNode root) {
if (root == null) return 0;
getDepth(root, 1);
return max;
}
}