classSolution{public:intmaxDepth(TreeNode* root){if(root==NULL)return0;//如果是空姐点,返回深度0int left =maxDepth(root->left);//左子树的最大深度int right =maxDepth(root->right);//右子树的最大深度return1+max(left, right);//中节点返回左右子树中大的深度+1,表示当前子树的最大深度}};
classSolution{public:intmaxDepth(Node* root){if(root==NULL)return0;int dep =0;for(auto& node : root->children){
dep =max(dep,maxDepth(node));}return dep+1;}};
classSolution{public:intcountNodes(TreeNode* root){if(root==NULL)return0;int left =countNodes(root->left);int right =countNodes(root->right);return1+left+right;}};