Check if two binary trees are structurally identical.
- User
Check if two binary trees are structurally identical.
Assistant
pythonclass TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def is_identical(p, q): """ Check if two binary trees are structurally identical. Args: p: Root of the first binary tree q: Root of the second binary tree Returns: bool: True if the trees are identical, False otherwise """ # Base cases: if both nodes are None, they're identical if p is None and q is None: return True # If only one is None, they're not identical if p is None or q is None: return False # Check current nodes have same value and same structure return (p.val == q.val and is_identical(p.left, q.left) and is_identical(p.right, q.right))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.