`
blue2048
  • 浏览: 177924 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

[leetcode]Maximum Depth of Binary Tree - java 递归

阅读更多
/**

 * 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;

    }

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics