forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrumbs22.cpp
More file actions
24 lines (22 loc) ยท 888 Bytes
/
crumbs22.cpp
File metadata and controls
24 lines (22 loc) ยท 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
์ ์์ํ ํ๋ฉด์ ๋ ํธ๋ฆฌ๊ฐ ๊ฐ์์ง ๋น๊ต
์ฌ๊ท์ ์ผ๋ก ํ์ํ๋ฏ๋ก
๋ ํธ๋ฆฌ์ ์์๋
ธ๋ ์ค ํ๋๋ผ๋ ์๋ค๋ฉด ํ์ถํ๋ค
p->val == q->val ์กฐ๊ฑด์ด ์๋ p->val != q->val ์กฐ๊ฑด์ ํ๋จํด์ผ
p->val๊ณผ q->val์ด ๊ฐ์ ๋ ๊ทธ ๋ค์ ์์๋
ธ๋๋ก ๋ด๋ ค๊ฐ๋ return๋ฌธ์ผ๋ก ๋น ์ง ์ ์๋ค
(p->val == q->val ์กฐ๊ฑด์ ์ฌ์ฉํ๋ฉด true๊ฐ ๋ฐํ๋๋ฏ๋ก ์ค๊ฐ์ ์ข
๊ฒฐ๋๋ค)
์๊ฐ๋ณต์ก๋๋ ํธ๋ฆฌ์ ๋์ด์ ๊ฐ๋ค
๋ค๋ฅธ ์ถ๊ฐ์ ๊ณต๊ฐ์ ์ฌ์ฉํ์ง ์์ผ๋ฏ๋ก ๊ณต๊ฐ๋ณต์ก๋๋ O(1)์ด๋ค
*/
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if (!p && !q)
return (true);
if (!p || !q)
return (false);
if (p->val != q->val)
return (false);
return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
}
};