и посмотреть медиа
Golang | LeetCode
и посмотреть медиа
LeetCode tasks in Golang. Solutions with code and explanations.
LeetCode tasks in Golang. Solutions with code and explanations.
AI bot for text-to-speech and voice cloning in Telegram. Create audio using neural network quickly and easily.
Channel with solutions to popular LeetCode tasks in Go. Focus on optimized algorithms, efficient memory usage, and idiomatic code. Helps prepare for technical interviews in companies that use Golang.
Задача: 1302. Deepest Leaves Sum Сложность: medium Дано корень бинарного дерева, вернуть сумму значений его самых глубоких листьев. Пример: Input: root = [1,2,3,4,5,null,6,7,null,null,null,null,8] Output: 15 👨💻 Алгоритм: 1⃣Поместите корень в стек. 2⃣Пока стек не пуст. Извлеките узел из стека и обновите текущее число. Если узел является листом, обновите сумму самых глубоких листьев deepest_sum. Поместите правый и левый дочерние узлы в стек. 3⃣Верните deepest_sum. 😎 Решение: package main func deepestLeavesSum(root *TreeNode) int { deepestSum, depth := 0, 0 stack := [][2]interface{}{{root, 0}} for len(stack) > 0 { node, currDepth := stack[len(stack)-1][0].(*TreeNode), stack[len(stack)-1][1].(int) stack = stack[:len(stack)-1] if node.Left == nil && node.Right == nil { if depth < currDepth { deepestSum = node.Val depth = currDepth } else if depth == currDepth { deepestSum += node.Val } } else { if node.Right != nil { stack = append(stack, [2]interface{}{node.Right, currDepth + 1}) } if node.Left != nil { stack = append(stack, [2]interface{}{node.Left, currDepth + 1}) } } } return deepestSum } Ставь 👍 и забирай 📚 Базу знаний
Открыть канал и посмотреть медиаOnly registered users can share their opinion.
Be the first to share your impression of this resource!