一、二叉树最大高度
class Solution { public:int maxDepth(TreeNode* root) {if(root==nullptr)return 0;int left= maxDepth(root->left)+1;int right=maxDepth(root->right)+1;return left>right?left:right;} };
二、n叉树最大高度
class Solution {
public:int maxDepth(Node* root) {if (root == nullptr) {return 0;}int maxChildDepth = 0;vector<Node *> children = root->children;for (auto child : children) {int childDepth = maxDepth(child);maxChildDepth = max(maxChildDepth, childDepth);}return maxChildDepth + 1;}
};