> For the complete documentation index, see [llms.txt](https://jenhsuan.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jenhsuan.gitbook.io/algorithm/leetcode/100.-same-tree-easy.md).

# 100. Same Tree (Easy)

## 1.問題&#x20;

* 給予兩個binary tree, 決定是否相同

![](/files/-LHwy5nCMWx9AalOC573)

## 2.想法&#x20;

* Recursive + back tracking

## 3.程式碼

```
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
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 isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
        }
        
        return false;
    }
};
```

## 4.Performance

![](/files/-LHwyq2gqpBU_52lXf6Z)
