LintCode-劍指Offer-(97)二叉樹的最大深度
阿新 • • 發佈:2019-01-22
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */ class Solution { public: /** * @param root: The root of binary tree. * @return: An integer */ int maxDepth(TreeNode *root) { // write your code here maxD(root,1); return maxdepth; } int maxdepth = 0; void maxD(TreeNode* node,int d){ if (node==NULL)return; if (maxdepth<d)maxdepth = d; maxD(node->left,d+1); maxD(node->right,d+1); } };