и посмотреть медиа
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.
Задача: 958. Check Completeness of a Binary Tree Сложность: medium Дан корень бинарного дерева, определите, является ли оно полным бинарным деревом. В полном бинарном дереве каждый уровень, за исключением, возможно, последнего, полностью заполнен, и все узлы на последнем уровне расположены как можно левее. На последнем уровне h может быть от 1 до 2^h узлов включительно. Пример: Input: root = [1,2,3,4,5,6] Output: true Explanation: Every level before the last is full (ie. levels with node-values {1} and {2, 3}), and all nodes in the last level ({4, 5, 6}) are as far left as possible. 👨💻 Алгоритм: 1⃣Если корень дерева равен null, верните true. 2⃣Инициализируйте переменную nullNodeFound как false для отслеживания того, встречался ли уже null-узел. Создайте очередь и поместите в неё корень дерева. 3⃣Пока очередь не пуста: Извлеките первый элемент из очереди. Если элемент равен null, установите nullNodeFound в true. Если элемент не равен null, проверьте, встречался ли уже null-узел. Если nullNodeFound равен true, верните false. В противном случае добавьте в очередь левого и правого потомков текущего узла. 😎 Решение: func isCompleteTree(root *TreeNode) bool { if root == nil { return true } queue := []*TreeNode{root} nullNodeFound := false for len(queue) > 0 { node := queue[0] queue = queue[1:] if node == nil { nullNodeFound = true } else { if nullNodeFound { return false } queue = append(queue, node.Left) queue = append(queue, node.Right) } } return true } Ставь 👍 и забирай 📚 Базу знаний
Открыть канал и посмотреть медиаOnly registered users can share their opinion.
Be the first to share your impression of this resource!