image.png

    1. /**
    2. * Definition for a binary tree node.
    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 max = 0;
    13. int treeDepth(TreeNode* root) {
    14. if(root == NULL) return 0;
    15. return max(treeDepth(root->left), treeDepth(root->right)) + 1;
    16. }
    17. };