class Solution { public: int maxDepth(TreeNode* root) { if (!root) return 0; return 1 + max(maxDepth(root->left), maxDepth(root->right)); } };
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int maxDepth(TreeNode* root) { return DFS(root,0); } int DFS(TreeNode* root,int cnt){ if(root == NULL) return cnt; return max(DFS(root->left,cnt+1),DFS(root->right,cnt+1)); } };
//这题写的比较满意