Python、C++、JavaScript、SQL、TypeScript の多様な LeetCode ソリューションを探索してください。面接の準備、学習、複数のプログラミング言語でのコードの練習に最適です。 Github リポジトリ リンク
パイソン
# バイナリ ツリー ノードの定義。 # クラス TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right クラス ソリューション: def maxDepth(self, root: Optional[TreeNode]) -> int: def longestPath(node: Optional[TreeNode]): if not node: return 0 leftPath = longestPath(node.left) rightPath = longestPath(node.right) return max(leftPath, rightPath)+1 return longestPath(root)