https://leetcode.com/problems/same-tree/description/
0ms
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}
if (p == null || q == null) {
return false;
}
if (p.val != q.val) {
return false;
}
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
트리가 어렵게 느껴지는 와중에 단번에 풀어버렸다.. 재귀는 풀수록 모르겠다..
'코딩테스트' 카테고리의 다른 글
268. Missing Number (0) | 2024.02.17 |
---|---|
283. Move Zeroes (0) | 2024.02.17 |
409. Longest Palindrome (0) | 2024.02.17 |
9. Palindrome Number (0) | 2024.02.16 |
234. Palindrome Linked List (0) | 2024.02.16 |