Swift: 计算二叉树的最大深度
/**
- Definition for a binary tree node.
- public class TreeNode {
-
public var val: Int -
public var left: TreeNode? -
public var right: TreeNode? -
public init() { self.val = 0; self.left = nil; self.right = nil; } -
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; } -
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { -
self.val = val -
self.left = left -
self.right = right -
} - } */ class Solution { func maxDepth(_ root: TreeNode?) -> Int { if root == nil { return 0 } return max(maxDepth(root.left), maxDepth(root.right)) + 1 } }//如果仍然报(TreeNode已定义且要导入的库都已导入)错误,请检查您的代码是否正确导入和引用了TreeNode类。
原文地址: https://www.cveoy.top/t/topic/qflo 著作权归作者所有。请勿转载和采集!