Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
19 lines (17 loc) · 745 Bytes

File metadata and controls

19 lines (17 loc) · 745 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package ch14;
import datatype.TreeNode;
public class P51_1 {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
// 한쪽이 널이면 널이 아닌 노드를 리턴한다.
if (root1 == null) return root2;
if (root2 == null) return root1;
// 병합된 노드 생성
TreeNode node = new TreeNode(root1.val + root2.val);
// 병합된 노드의 자식 노드로 기존 노드 왼쪽 자식 노드 DFS 진행
node.left = mergeTrees(root1.left, root2.left);
// 병합된 노드의 자식 노드로 기존 노드 오른쪽 자식 노드 DFS 진행
node.right = mergeTrees(root1.right, root2.right);
// 병합된 노드 리턴
return node;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.