topic
[Input binary tree, find the depth of the tree. From the root node to a leaf node sequentially passes (including the root, leaf node) tree forming a path, the length of the longest path depth of the tree]
1, analysis
- The depth of the binary tree for a depth of about 1 plus each subtree (if present around subtree), and then processed recursively.
2, the code
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
int TreeDepth(TreeNode* pRoot)
{
if(pRoot==nullptr)
return 0;
int leftCount = TreeDepth(pRoot->left);
int rightCount = TreeDepth(pRoot->right);
return (leftCount>rightCount)?(leftCount+1):(rightCount+1);
}
};