Same Tree

题目:

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

 

 

思路:和symmetric tree非常类似的递归解法

1     public boolean isSameTree(TreeNode p, TreeNode q) 
2     {
3         if(p==null&&q==null) return true;
4         if(p==null||q==null) return false;
5         return (p.val == q.val) && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
6         
7     }

 

posted @ 2015-08-02 08:15  Hygeia  阅读(102)  评论(0编辑  收藏  举报