首页 > 代码库 > [LeetCode]Maximum Depth of Binary Tree

[LeetCode]Maximum Depth of Binary Tree

算法:递归

原理:递归遍历二叉树,从底向上计数

/**
 * 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 (null == root) {
	        	return 0;
	        }
	        
	        int left = maxDepth(root.left);
	        int right = maxDepth(root.right);
	        return (left > right ? left+1 : right+1);
	    }
	}