100. 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.

题目含义:判断两个二进制树结构是否相等

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

 

posted @ 2017-10-21 17:14  daniel456  阅读(75)  评论(0编辑  收藏  举报