标签:
/** * 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) { if(root==NULL) return 0; return max(dep(root->left,1),dep(root->right,1)); } int dep(TreeNode* root,int pre_h){ if(root==NULL) return pre_h; if(root->left ==NULL && root->right ==NULL) return pre_h+1; return max(dep(root->left,pre_h+1),dep(root->right,pre_h+1)); } };
标签:
原文地址:http://www.cnblogs.com/julie-yang/p/4683316.html