首页 > 代码库 > leetcode Maximum Depth of Binary Tree

leetcode Maximum Depth of Binary Tree

返回树的深度。

递归:

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    int maxDepth(TreeNode *root)     {        if (!root) return 0;        int l = maxDepth(root -> left);        int r = maxDepth(root -> right);        return max(l + 1, r + 1);    }};

 

leetcode Maximum Depth of Binary Tree