都能求最小了,就能求最大:
int maxDepth(TreeNode* root) { if (root == nullptr) { return 0; } constexpr int MIN_DEPTH = 0; constexpr int initLayerNum = 1; function<int(TreeNode*&, int)> check; check = [&](TreeNode*& node, int num) { if (node == nullptr) { return MIN_DEPTH; } if (node->left == nullptr && node->right == nullptr){ return num; } else{ return max(check(node->left, num + 1), check(node->right, num + 1)); } }; return check(root, initLayerNum); }
时间: 2024-11-13 15:28:29