Python•minimum-depth-of-binary-tree/synced-solution.py
class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return class="syntax-number">0
if not root.left:
return class="syntax-number">1 + self.minDepth(root.right)
if not root.right:
return class="syntax-number">1 + self.minDepth(root.left)
return class="syntax-number">1 + min(self.minDepth(root.left), self.minDepth(root.right))
C++•minimum-depth-of-binary-tree/synced-solution.cpp
class Solution {
public:
int minDepth(TreeNode* root) {
if (!root) return class="syntax-number">0;
if (!root->left) return class="syntax-number">1 + minDepth(root->right);
if (!root->right) return class="syntax-number">1 + minDepth(root->left);
return class="syntax-number">1 + min(minDepth(root->left), minDepth(root->right));
}
};
TypeScript•minimum-depth-of-binary-tree/synced-solution.ts
function minDepth(root: TreeNode | null): number {
if (!root) return class="syntax-number">0;
if (!root.left) return class="syntax-number">1 + minDepth(root.right);
if (!root.right) return class="syntax-number">1 + minDepth(root.left);
return class="syntax-number">1 + Math.min(minDepth(root.left), minDepth(root.right));
}