Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions same-tree/DaleSeo.rs
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: DFS
  • 설명: 이 코드는 재귀를 이용하여 트리의 노드를 깊이 우선 탐색하며 두 트리의 구조와 값을 비교하는 방식으로 동작합니다. 따라서 DFS 패턴에 속합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(h)

피드백: 트리의 모든 노드를 한 번씩 방문하는 재귀 방식으로, 각 노드에 대해 상수 시간 비교와 재귀 호출이 이루어집니다. 공간 복잡도는 재귀 호출 스택의 깊이인 트리의 높이 h에 비례합니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// TC: O(n)
// SC: O(n)
use std::cell::RefCell;
use std::rc::Rc;

impl Solution {
pub fn is_same_tree(
p: Option<Rc<RefCell<TreeNode>>>,
q: Option<Rc<RefCell<TreeNode>>>,
) -> bool {
match (p, q) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust 에는 match 라는 메소드가있군요. 한 주도 고생많으셨습니다!

(None, None) => true,
(Some(a), Some(b)) => {
let a = a.borrow();
let b = b.borrow();
a.val == b.val
&& Self::is_same_tree(a.left.clone(), b.left.clone())
&& Self::is_same_tree(a.right.clone(), b.right.clone())
}
_ => false,
}
}
}
Loading