From 7f1ae86935bb8063b5dacdf1b75914d158edaeba Mon Sep 17 00:00:00 2001 From: ductnn Date: Sun, 14 Apr 2024 17:54:26 +0700 Subject: [PATCH] add sol --- leetcode/404.SumofLeftLeaves/sol.go | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 leetcode/404.SumofLeftLeaves/sol.go diff --git a/leetcode/404.SumofLeftLeaves/sol.go b/leetcode/404.SumofLeftLeaves/sol.go new file mode 100644 index 0000000..9a2f4e4 --- /dev/null +++ b/leetcode/404.SumofLeftLeaves/sol.go @@ -0,0 +1,31 @@ +// https://leetcode.com/problems/sum-of-left-leaves + +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 sumOfLeftLeaves(root *TreeNode) int { + if root == nil { + return 0 + } + res := 0 + if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil { + res += root.Left.Val + } + res += sumOfLeftLeaves(root.Left) + res += sumOfLeftLeaves(root.Right) + return res +}