https://leetcode.com/problems/same-tree/description/
주어진 두개의 트리가 동일한지 확인하라.
두 트리의 원소의 모든 값을 대조한다.
코드
더보기
void isSameTreeHelper(TreeNode* nodeA, TreeNode* nodeB, bool& same)
{
if(!same) return;
if(nodeA==nullptr && nodeB==nullptr)
return;
if((nodeA==nullptr && nodeB!=nullptr) || (nodeA!=nullptr && nodeB==nullptr))
{
same = false;
return;
}
if(nodeA->val != nodeB->val)
{
same = false;
return;
}
isSameTreeHelper(nodeA->left, nodeB->left, same);
isSameTreeHelper(nodeA->right, nodeB->right, same);
}
bool isSameTree(TreeNode* p, TreeNode* q)
{
bool same = true;
isSameTreeHelper(p, q, same);
return same;
}
결과
'Leetcode > NeetCode' 카테고리의 다른 글
[BinaryTree][Medium] 235. Lowest Common Ancestor of a Binary Search Tree (0) | 2024.07.17 |
---|---|
[BinaryTree][Easy] 572. Subtree of Another Tree (0) | 2024.07.17 |
[BinaryTree][Easy] 110. Balanced Binary Tree (0) | 2024.07.17 |
[BinaryTree][Easy] 543. Diameter of Binary Tree (0) | 2024.07.17 |
[BinaryTree][Easy] 104. Maximum Depth of Binary Tree (0) | 2024.07.17 |