【LeetCode】104. Maximum Depth of Binary Tree

问题描述

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

求树的深度

代码

class Solution {
public:
    int maxDepth(TreeNode* root) {
        return depth(root, 0);
    }
    
    int depth(TreeNode* p, int level) {
		if(p==0) return level;
		level++;
		int l1 = depth(p->left, level);
		int l2 = depth(p->right, level);
		return l1>l2?l1:l2;
    }
};