GOLANG_INTERVIEW Telegram 187
Задача. Слияние двух бинарных деревьев

Сложность: Лёгкая

Условие задачи: Даны два бинарных дерева, необходимо осуществить их наложение друг на друга и вывод результатов в новом дереве.

Примечание: Наложение представляет из себя суммирование соответствующих значений из узлов двух деревьев.

Пример:

Ввод:
root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
Вывод:
[3,4,5,5,4,null,7]

Ввод:
root1 = [1], root2 = [1,2]
Вывод:
[2,2]

Решение

Пишите свое мнение в комментариях👇

func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
if root1 == nil {
return root2
}

if root2 == nil {
return root1
}

root1.Val += root2.Val
root1.Left = mergeTrees(root1.Left, root2.Left)
root1.Right = mergeTrees(root1.Right, root2.Right)
return root1
}


@golang_interview
👍6🔥3🥰1



tgoop.com/golang_interview/187
Create:
Last Update:

Задача. Слияние двух бинарных деревьев

Сложность: Лёгкая

Условие задачи: Даны два бинарных дерева, необходимо осуществить их наложение друг на друга и вывод результатов в новом дереве.

Примечание: Наложение представляет из себя суммирование соответствующих значений из узлов двух деревьев.

Пример:

Ввод:
root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
Вывод:
[3,4,5,5,4,null,7]

Ввод:
root1 = [1], root2 = [1,2]
Вывод:
[2,2]

Решение

Пишите свое мнение в комментариях👇

func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
if root1 == nil {
return root2
}

if root2 == nil {
return root1
}

root1.Val += root2.Val
root1.Left = mergeTrees(root1.Left, root2.Left)
root1.Right = mergeTrees(root1.Right, root2.Right)
return root1
}


@golang_interview

BY Golang вопросы собеседований




Share with your friend now:
tgoop.com/golang_interview/187

View MORE
Open in Telegram


Telegram News

Date: |

Concise While some crypto traders move toward screaming as a coping mechanism, many mental health experts have argued that “scream therapy” is pseudoscience. Scientific research or no, it obviously feels good. For crypto enthusiasts, there was the “gm” app, a self-described “meme app” which only allowed users to greet each other with “gm,” or “good morning,” a common acronym thrown around on Crypto Twitter and Discord. But the gm app was shut down back in September after a hacker reportedly gained access to user data. The optimal dimension of the avatar on Telegram is 512px by 512px, and it’s recommended to use PNG format to deliver an unpixelated avatar. How to create a business channel on Telegram? (Tutorial)
from us


Telegram Golang вопросы собеседований
FROM American