- This is a summary of Tree Problems in LeetCode. Including the thought, code and some tricks.
Preorder
100. Same Tree
- 可套用模板,双pre
1
2
3
4
5
6public 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);
}