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.
點擊打開原題鏈接
這個題劍指offer裡也有,簡單的遞歸即可,代碼很清晰:
class Solution { public: int maxDepth(TreeNode *root) { if (root == NULL) { return 0; } else { int a = maxDepth(root->left) + 1; int b = maxDepth(root->right) + 1; if (a > b) { return a; } else { return b; } } } private: //static int depth; };