1、minimum-depth-of-binary-tree
发布时间:2020-12-14 03:45:48 所属栏目:大数据 来源:网络整理
导读:题目描述 Given a binary tree,find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. ? int run(TreeNode * root){ if (root == nullptr) return 0 ; if (root-lef
题目描述
Given a binary tree,find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
?
int run(TreeNode *root){ if (root == nullptr) return 0; if (root->left == nullptr) { return run(root->right) + 1; } if (root->right == nullptr) { return run(root->left) + 1; } int leftDepth = run(root->left); int rightDepth = run(root->right); return (leftDepth <= rightDepth) ? (leftDepth + 1) : (rightDepth + 1); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |