Algo-Sim › Binary Trees & BST
Same Tree
Easy · Binary Trees & BST · step through it in Java, Python and C++ on your own input
Two trees are the same when their roots match and their left and right subtrees are the same: recurse on both trees in step.
Problem
Given the roots of two binary trees p and q, return true if they are the same: identical in shape, with equal values in matching positions. Two empty trees are the same.
Constraints
- 0 ≤ nodes in each tree ≤ 100
- -10⁴ ≤ Node.val ≤ 10⁴
Edge cases to test
- Both empty:
true - One empty
- Same values, different shapes
Follow-up questions
- How do you compare without recursion?
- Walk both trees with one queue of node pairs.
- How do you check many trees for duplicates?
- Serialise each subtree to a string or id and count them in a hash map (LC 652): O(n) with ids.
Solution
Java
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);
}Python
def is_same_tree(p, q):
if not p and not q:
return True
if not p or not q:
return False
if p.val != q.val:
return False
return (is_same_tree(p.left, q.left)
and is_same_tree(p.right, q.right))C++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
if (p == nullptr && q == nullptr)
return true;
if (p == nullptr || q == nullptr)
return false;
if (p->val != q->val)
return false;
return isSameTree(p->left, q->left)
&& isSameTree(p->right, q->right);
}
};
More Binary Trees & BST problems
- Preorder Traversal (Recursive) (Easy)
- Inorder Traversal (Recursive) (Easy)
- Postorder Traversal (Recursive) (Easy)
- Preorder Traversal (Iterative) (Easy)
- Inorder Traversal (Iterative) (Easy)
- Postorder Traversal (Iterative) (Easy)
- Binary Tree Level Order Traversal (Medium)
- Binary Tree Zigzag Level Order Traversal (Medium)
- Binary Tree Right Side View (Medium)
- Vertical Order Traversal of a Binary Tree (Hard)
- Maximum Depth of Binary Tree (Easy)
- Diameter of Binary Tree (Easy)
- Balanced Binary Tree (Easy)
- Invert Binary Tree (Easy)
- Flatten Binary Tree to Linked List (Medium)
- Symmetric Tree (Easy)
- Subtree of Another Tree (Easy)
- Path Sum (Easy)
- Path Sum II (Medium)
- Path Sum III (Medium)
- Count Good Nodes in Binary Tree (Medium)
- Lowest Common Ancestor of a Binary Tree (Medium)
- Kth Ancestor of a Tree Node (Hard)
- All Nodes Distance K in Binary Tree (Medium)
- Validate Binary Search Tree (Medium)
- Search in a Binary Search Tree (Easy)
- Insert into a Binary Search Tree (Medium)
- Delete Node in a BST (Medium)
- Kth Smallest Element in a BST (Medium)
- Binary Search Tree Iterator (Medium)
- Convert Sorted Array to Binary Search Tree (Easy)
- AVL Tree Insert (Hard)
- Construct Binary Tree from Preorder and Inorder Traversal (Medium)
- Binary Tree Maximum Path Sum (Hard)
- Serialize and Deserialize Binary Tree (Hard)