-
[알고리즘] #Merge Two Binary Trees■ Algorithm 2019. 7. 6. 20:56
출처: https://leetcode.com/problems/merge-two-binary-trees/
Merge Two Binary Trees - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.
You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.
Note: The merging process must start from the root nodes of both trees.
풀이
두 개의 트리의 val 값을 합치는 문제이다. 보자마자 재귀함수로 순환하면서 풀면 된다고 생각했다.
첫 번째 트리를 기준으로 두 번째 트리의 값을 첫 번째 트리에 더해주면서 순환하면 된다.
'■ Algorithm' 카테고리의 다른 글
[알고리즘] #Invert Binary Tree (0) 2019.07.07 [알고리즘] #Single Number (0) 2019.07.06 [알고리즘] #최고의 집합 (0) 2019.06.26 [알고리즘] 방문 길이 (0) 2019.06.24 [알고리즘] #체육복 (0) 2019.05.28