From 94a13f3453b22f77301ff26eeb7804946e09b088 Mon Sep 17 00:00:00 2001 From: ductnn Date: Sun, 17 Dec 2023 18:08:50 +0700 Subject: [PATCH] add sol --- .../maximumDepthOfBinaryTree.go | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 leetcode/leetcode75/BinaryTree-DFS/104.MaximumDepthOfBinaryTree/maximumDepthOfBinaryTree.go diff --git a/leetcode/leetcode75/BinaryTree-DFS/104.MaximumDepthOfBinaryTree/maximumDepthOfBinaryTree.go b/leetcode/leetcode75/BinaryTree-DFS/104.MaximumDepthOfBinaryTree/maximumDepthOfBinaryTree.go new file mode 100644 index 0000000..d78402d --- /dev/null +++ b/leetcode/leetcode75/BinaryTree-DFS/104.MaximumDepthOfBinaryTree/maximumDepthOfBinaryTree.go @@ -0,0 +1,35 @@ +package main + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +func maxDepth(root *TreeNode) int { + if root == nil { + return 0 + } + left := maxDepth(root.Left) + right := maxDepth(root.Right) + + return 1 + max(left, right) +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} + +func main() {}