Maximum Depth of Binary Tree

题目链接:http://oj.leetcode.com/problems/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.

解法:递归;父节点的深度等于左子树/右子树中深度大者+1

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int maxDepth(TreeNode *root) {
13         // IMPORTANT: Please reset any member data you declared, as
14         // the same Solution instance will be reused for each test case.
15         int l, r;
16         if (root == NULL)
17             return 0;
18         else if ((l = maxDepth(root->left)) > (r = maxDepth(root->right)))
19             return l + 1;
20         else
21             return r + 1;
22     }
23 };
原文地址:https://www.cnblogs.com/hbiner/p/3403942.html