题目:104
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == NULL) return 0;
return 1+max(maxDepth(root->left),maxDepth(root->right));
}
};
题目:559
class Solution {
public:
int maxDepth(Node* root) {
if(rootNULL) return 0;
int depth=0;
for(int i=0;ichildren.size();++i){
depth=max(depth,maxDepth(root->children[i]));
}
return depth +1;
}
};
题目111
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == NULL) return 0;
if (root->left == NULL && root->right != NULL) {
return 1 + minDepth(root->right);
}
if (root->left != NULL && root->right == NULL) {
return 1 + minDepth(root->left);
}
return 1 + min(minDepth(root->left), minDepth(root->right));
}
};
题目222
class Solution {
public:
int countNodes(TreeNode* root) {
if(rootNULL) return 0;
int rightnum=countNodes(root->right);
int leftnum=countNodes(root->left);
return 1+leftnum+rightnum;
}
};