57.对称二叉树

迭代

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root==null){
            return true;
        }
        Deque<TreeNode> de=new LinkedList<>();
        TreeNode l,r;
        int le;
        de.offer(root.left);
        de.offer(root.right);
        while(!de.isEmpty()){
            l=de.pollFirst();
            r=de.pollLast();
            if(l==null&&r==null){
                continue;
            }
            if((l!=null&&r==null)||(l==null&&r!=null)||l.val!=r.val){
                return false;
            }
            if(l!=null&&r!=null){
                de.offerFirst(l.left);
                de.offerFirst(l.right);
                de.offerLast(r.right);
                de.offerLast(r.left);
            }
        }
        return true;
    }
}
class Solution(object):
    def isSymmetric(self, root):
        if root is None:
            return True
        de=collections.deque()
        de.append(root.left)
        de.append(root.right)
        while de:
            l=de.popleft()
            r=de.pop()
            if l is None and r is None:
                continue
            if (l is None and r) or (l and r is None) or l.val!=r.val:
                return False
            if l and r:
                de.appendleft(l.left)
                de.appendleft(l.right)
                de.append(r.right)
                de.append(r.left)
        return True

 

递归(本方法非原创,来自代码随想录

//本方法非原创,来自https://programmercarl.com/0101.%E5%AF%B9%E7%A7%B0%E4%BA%8C%E5%8F%89%E6%A0%91.html#%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E7%89%88%E6%9C%AC
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root==null){
            return true;
        }
        return Compare(root.left,root.right);
    }
    private boolean Compare(TreeNode left, TreeNode right) {

        if(left==null&&right!=null){
            return false;
        }
        if(left!=null&&right==null){
            return false;
        }
        if(left==null&&right==null){
            return true;
        }
        if(left.val!=right.val){
            return false;
        }
        // 比较外侧
        boolean compareOutside=Compare(left.left,right.right);
        // 比较内侧
        boolean compareInside=Compare(left.right,right.left);
        return compareOutside && compareInside;
    }
}
#本方法非原创,来自https://programmercarl.com/0101.%E5%AF%B9%E7%A7%B0%E4%BA%8C%E5%8F%89%E6%A0%91.html#%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E7%89%88%E6%9C%AC
class Solution(object):
    def isSymmetric(self, root):
        if root is None:
            return True
        def Compare(left,right):
            if left is None and right:
                return False
            if left and right is None:
                return False
            if left is None and right is None:
                return True
            if left.val!=right.val:
                return False;
            #比较外侧
            compareOutside=Compare(left.left,right.right)
            #比较内侧
            compareInside=Compare(left.right,right.left)
            return compareOutside and compareInside
        return Compare(root.left,root.right)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值