//求一颗二叉树的最大深度 求高度:后序遍历 求深度:前序遍历 int maxDepth(TreeNode* root) { return root ? 1 + max(maxDepth(root->left), maxDepth(root->right)) : 0; } //求一颗二叉树的最小深度(实质上是后序遍历) int minDepth(TreeNode* root) { if (!root) return 0; //int leftDepth = minDepth(root->left); //int rightDepth = minDepth(root->right); //若左子树为空,右子树不为空 if (!root->left && root->right) { return 1 + minDepth(root->right); } //若左子树不为空,右子树为空 else if (root->left && !root->right) { return 1 + minDepth(root->left); } else { return 1 + min(minDepth(root->left), minDepth(root->right)); } }标签:right,return,C++,minDepth,二叉树,深度,root,left From: https://www.cnblogs.com/smartlearn/p/18056165