/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(null == p || null == q){
if(null == p && null == q)
return true;
return false;
}
if( p.val != q.val )
return false;
return (isSameTree(p.left,q.left) && isSameTree(p.right,q.right));
}
}
判断条件的逻辑:1.先判断为空的情况;2.判断值不相同的情况;3.分别递归判断左右子树same情况。
Same Tree
最新推荐文章于 2020-08-05 00:52:41 发布