二叉树深度
二叉树的深度
http://www.nowcoder.com/questionTerminal/435fb86331474282a3499955f0a41e8b
dfs遍历每一条路径,比较其大小即可。
public class Solution { int max = 0; public void dfs(TreeNode root, int deepth){ if(root==null){ if(deepth>max){ max = deepth; } return; } dfs(root.left, deepth+1); dfs(root.right, deepth+1); } public int TreeDepth(TreeNode root) { dfs(root, 0); return max; } }