Same Tree
简单题
ref“使用的是先序遍历,算法的复杂度跟遍历是一致的,如果使用递归,时间复杂度是O(n),空间复杂度是O(logn)。” by codeganker http://blog.csdn.net/linhuanmars/article/details/22839819
•Time to search for an item in a binary tree– O(log n)
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); }