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);
    }

 

posted @ 2015-06-10 03:03  世界到处都是小星星  阅读(133)  评论(0编辑  收藏  举报