/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int maxDepth(TreeNode root) { if(root==null){ return 0; } return maxDep(root, 1); } private int maxDep(TreeNode node, int pDepth){ int leftDep = pDepth; int rightDep = pDepth; int cDepth = ++pDepth; if(node.left!=null){ leftDep = maxDep(node.left, cDepth); } if(node.right!=null){ rightDep = maxDep(node.right, cDepth); } return leftDep>rightDep?leftDep:rightDep; } }
[leetcode]Maximum Depth of Binary Tree - java 递归
猜你喜欢
转载自blue2048.iteye.com/blog/2195844
今日推荐
周排行