и посмотреть медиа
Golang LeetCode
и посмотреть медиа
Tareas de LeetCode en Golang. Soluciones con código y explicaciones.
Tareas de LeetCode en Golang. Soluciones con código y explicaciones.
bot de IA para clonar texto a voz y voz en Telegram. Cree audio utilizando la red neuronal de forma rápida y fácil.
Canal con soluciones para tareas populares de LeetCode en Go. Concéntrate en algoritmos optimizados, uso eficiente de memoria y código idiomático. Ayuda a preparar entrevistas técnicas en empresas que utilizan 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 } Ставь 👍 и забирай 📚 Базу знаний
Открыть канал и посмотреть медиаSolo los usuarios registrados pueden compartir su opinión.
¡Sé el primero en compartir tu experiencia con este recurso!