100. Same Tree ¶ Time: $O(n)$ Space: $O(h)$ C++JavaPython 1 2 3 4 5 6 7 8 9 10class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if (!p || !q) return p == q; return p->val == q->val && // isSameTree(p->left, q->left) && // isSameTree(p->right, q->right); } }; 1 2 3 4 5 6 7 8 9class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if (p == null || q == null) return p == q; return p.val == q.val && // isSameTree(p.left, q.left) && // isSameTree(p.right, q.right); } } 1 2 3 4 5 6 7class Solution: def isSameTree(self, p: TreeNode | None, q: TreeNode | None) -> bool: if not p or not q: return p == q return (p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right))