LeetCode 100 相同的树

时间:2020-07-22 10:59:49   收藏:0   阅读:92

Leetcode 100 相同的树

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        //不同时存在
        if((p==null && q!=null) || (p!=null && q==null))
            return false;
        //都存在但值不相同
        else if(p!=null && q!=null) {
            if(p.val != q.val) return false;
        }
        //都不存在
        else return true;
        
        //比较子节点
        boolean leftTree = isSameTree(p.left, q.left);
        boolean rightTree = isSameTree(p.right, q.right);

        return leftTree && rightTree; 
    }
}

原文:https://www.cnblogs.com/CodeSPA/p/13358856.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!