算法分析:这道题很简单,利用递归即可。
public class SameTree { public boolean isSameTree(TreeNode p, TreeNode q) { if(p == null) { return q == null; } if(q == null) { return p == null; } if(p.val == q.val) { return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); } else { return false; } } }