Skip to content

Commit e6e5eba

Browse files
committed
rivkode subtree of another tree
1 parent 7b3c0e5 commit e6e5eba

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
17+
/**
18+
* μ²˜μŒμ—λŠ” κ°€μž₯ μ•„λž˜μ˜ μ½”λ“œμ™€ 같이 BFS 둜 μ ‘κ·Όν•΄μ„œ ν’€λ €κ³  ν–ˆμ—ˆμ§€λ§Œ μ•Œκ³ λ¦¬μ¦˜ 선택이 잘λͺ»λ˜μ—ˆμŒμ„ κΉ¨λ‹¬μ•˜λ‹€.
19+
* 처음 BFS 둜 μ ‘κ·Όν–ˆλ˜ μ΄μœ λŠ” λ…Έλ“œμ˜ 값이 λ™μΌν•œ 경우λ₯Ό μ°Ύμ•˜μ„ λ•Œ κ·Έλ•ŒλΆ€ν„° 트리λ₯Ό μˆœνšŒν•˜λ©΄μ„œ μ„œλ‘œ λΉ„κ΅ν•˜λ©΄ 될 쀄 μ•Œμ•˜λ‹€.
20+
* ν•˜μ§€λ§Œ μ΄λ ‡κ²Œ 문제λ₯Ό ν’€λ €κ³  ν•˜λ‹ˆ μ—¬λŸ¬ μ—£μ§€ μΌ€μ΄μŠ€λ“€μ„ λ†“μ³€μ—ˆλ‹€. [1] [0] / [1] [1] λ“±λ“±
21+
* 그리고 같은 λ…Έλ“œμ˜ 값을 μ°Ύμ•˜λ‹€ ν•œλ“€ μ°Ύμ•˜μ„ λ•Œ λ‹€μ‹œ DFS둜 μ ‘κ·Όν•˜λŠ”? λ„ˆλ¬΄ λ³΅μž‘ν•œ 방식을 νƒν–ˆλ‹€. λ„ˆλ¬΄ λŒμ•„κ°”λ‹€. (μ΄λ•Œ λ­”κ°€ 잘λͺ»λ˜μ—ˆμŒμ„ λŠκΌˆλ‹€.)
22+
*
23+
* μ²˜μŒλΆ€ν„° DFSλ₯Ό μ„ νƒν–ˆμ–΄μ•Ό ν–ˆκ³  문제 νŠΉμ„±μƒ 쑰건듀이 μž¬κ·€μ μœΌλ‘œ μ •μ˜κ°€ λ˜μ—ˆλ‹€. A B κ°€ λ™μΌν•œκ°€ ? / A μ™Όμͺ½ μ„œλΈŒνŠΈλ¦¬κ°€ Bλ₯Ό ν¬ν•¨ν•˜λŠ”κ°€ ? / A 였λ₯Έμͺ½ μ„œλΈŒνŠΈλ¦¬κ°€ B λ₯Ό ν¬ν•¨ν•˜λŠ”κ°€ ?
24+
* μ—¬κΈ°μ„œ λΉ„κ΅ν•˜λ €λŠ” 2개의 λ…Έλ“œλ₯Ό 두고 λ˜‘κ°™μ€ 포인트λ₯Ό νƒμƒ‰ν•˜λ©° μ‹€μ œ μ‚¬λžŒμΈ λ‚΄κ°€ λΉ„κ΅ν•˜λ €λŠ” 것 처럼 λΉ„κ΅ν•˜λŠ”κ²ƒμ΄ μ•„λ‹ˆμ—ˆλ‹€.
25+
* ν¬μΈνŠΈλŠ” 문제처럼 rootκ°€ subRootλ₯Ό μ„œλΈŒνŠΈλ¦¬λ‘œ ν¬ν•¨ν•˜λŠ”κ°€ ? 이닀.
26+
* 이걸 3κ°€μ§€ 포인트둜 λ‚˜λˆ„μ–΄μ„œ root 와 subRootκ°€ κ·Έ 자체둜 λ™μΌν•œκ°€ ? / root.left와 SubRoot , root.right 와 subRootκ°€ λ™μΌν•œκ°€λ₯Ό λ³΄λŠ” κ²ƒμ΄μ—ˆλ‹€.
27+
* 그리고 μ—¬κΈ°μ„œ μž¬κ·€λ₯Ό μ‚¬μš©ν•΄μ„œ 값을 μ²΄ν¬ν•˜λŠ” 것이 ν¬μΈνŠΈμ˜€λ‹€.
28+
* BFS와 DFSλ₯Ό 잘 μ΄ν•΄ν•˜κ³  μ‚¬μš©ν•˜μž.
29+
*/
30+
31+
import java.util.*;
32+
33+
class Solution {
34+
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
35+
// subRoot κ°€ null 이면 trueλ₯Ό λ°˜ν™˜ν•œλ‹€. μ™œλƒν•˜λ©΄ λͺ¨λ“  λ…Έλ“œλ“€μ€ μžμ‹λ…Έλ“œλ‘œ null 을 κ°€μ§ˆ 수 있기 λ•Œλ¬Έμ΄λ‹€.
36+
if (subRoot == null) {
37+
return true;
38+
}
39+
40+
// subRoot κ°€ null 이 μ•„λ‹Œλ° root 만 null 이면 이 λ‘˜μ˜ λ…Έλ“œλŠ” λ‹€λ₯΄λ‹€λŠ” μ˜λ―Έμ΄λ‹€.
41+
if (root == null) {
42+
return false;
43+
}
44+
45+
// μœ„ 2개의 null 체크λ₯Ό ν•˜κ³  root와 subRoot κ°€ μ„œλ‘œ λ™μΌν•œμ§€ μ²΄ν¬ν•œλ‹€.
46+
if (isSameTree(root, subRoot)) {
47+
return true;
48+
}
49+
50+
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
51+
}
52+
53+
public boolean isSameTree(TreeNode root, TreeNode subRoot) {
54+
if (root == null && subRoot == null) {
55+
return true; // 2개의 λ…Έλ“œ value κ°€ λͺ¨λ‘ null 이면 true λ°˜ν™˜
56+
}
57+
58+
if (root == null || subRoot == null || root.val != subRoot.val) {
59+
return false; // 2κ°œμ€‘ ν•˜λ‚˜λ§Œ Null μ΄κ±°λ‚˜ 2개의 λ…Έλ“œ 값이 λ‹€λ₯Ό 경우 false λ°˜ν™˜
60+
}
61+
62+
// μœ„ 2개의 상황이 μ•„λ‹ˆλ©΄ λͺ¨λ‘ 같은 값을 κ°€μ§„ λ…Έλ“œλ“€μ˜ 값이 κ°™λ‹€λŠ” μ˜λ―Έμ΄λ―€λ‘œ μž¬κ·€μ μœΌλ‘œ μžμ‹λ“€μ„ 체크
63+
return isSameTree(root.left, subRoot.left) && isSameTree(root.right, subRoot.right);
64+
}
65+
}
66+
67+
// import java.util.*;
68+
69+
// class Solution {
70+
// static boolean result;
71+
// public boolean isSubtree(TreeNode root, TreeNode subRoot) {
72+
// result = true;
73+
74+
// Queue<TreeNode> queue = new LinkedList<>();
75+
// queue.offer(root);
76+
// int subRootValue = subRoot.val;
77+
// if (root == null && subRoot == null) {
78+
// return true;
79+
// }
80+
81+
// if (root.left == null && root.right == null && subRoot.left == null && subRoot.right == null) {
82+
// if (root.val == subRootValue) {
83+
// return true;
84+
// } else {
85+
// return false;
86+
// }
87+
// }
88+
89+
// while (!queue.isEmpty()) {
90+
// TreeNode qRoot = queue.poll();
91+
// if (qRoot.val == subRootValue) {
92+
// result = checkEqual(qRoot, subRoot);
93+
// break;
94+
// }
95+
96+
// if (qRoot.left != null) {
97+
// if (qRoot.left.val == subRootValue) {
98+
// result = checkEqual(qRoot.left, subRoot);
99+
// break;
100+
// }
101+
// queue.offer(qRoot.left);
102+
// }
103+
104+
// if (qRoot.right != null) {
105+
// if (qRoot.right.val == subRootValue) {
106+
// result = checkEqual(qRoot.right, subRoot);
107+
// break;
108+
// }
109+
// queue.offer(qRoot.right);
110+
// }
111+
// }
112+
113+
// return result;
114+
// }
115+
116+
// public boolean checkEqual(TreeNode root, TreeNode subRoot) {
117+
// if (root == null && subRoot == null) {
118+
// return true; // 2개의 λ…Έλ“œ value κ°€ λͺ¨λ‘ null 이면 true λ°˜ν™˜
119+
// }
120+
121+
// if (root == null || subRoot == null || root.val != subRoot.val) {
122+
// return false; // 2κ°œμ€‘ ν•˜λ‚˜λ§Œ Null μ΄κ±°λ‚˜ 2개의 λ…Έλ“œ 값이 λ‹€λ₯Ό 경우 false λ°˜ν™˜
123+
// }
124+
125+
// // μœ„ 2개의 상황이 μ•„λ‹ˆλ©΄ λͺ¨λ‘ 같은 값을 κ°€μ§„ λ…Έλ“œλ“€μ˜ 값이 κ°™λ‹€λŠ” μ˜λ―Έμ΄λ―€λ‘œ μž¬κ·€μ μœΌλ‘œ μžμ‹λ“€μ„ 체크
126+
127+
// return checkEqual(root.left, subRoot.left) && checkEqual(root.right, subRoot.right);
128+
// }
129+
// }

0 commit comments

Comments
Β (0)