当前位置: 首页 > article >正文

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)


http://www.kler.cn/news/336597.html

相关文章:

  • 利用SpringBoot框架开发星之语明星周边商城
  • 使用树莓派搭建音乐服务器
  • 【C#生态园】构建安全可靠的身份验证:六种C# OAuth认证库全面比较
  • 数学公式编辑器免费版下载,mathtype和latex哪个好用
  • RestClientException异常
  • 代码随想录算法训练营Day26 | 669. 修剪二叉搜索树、108.将有序数组转换为二叉搜索树、538.把二叉搜索树转换为累加树
  • Spring MVC的工作原理及配置。Spring Boot的自动配置与快速开发。
  • 毕业设计——物联网设备管理系统后台原型设计
  • 数据库语句优化
  • 《Linux从小白到高手》理论篇:深入理解Linux的网络管理
  • 六、Java 基础语法(下)
  • Oracle中ADD_MONTHS()函数详解
  • Rspamd:开源垃圾邮件过滤系统
  • 基于Rational Rose 做的UML图
  • 【电路基础 · 3】实际电压源 实际电流源;两种电源的等效情况;戴维南模型 诺顿模型(自用)
  • java集合框架都有哪些
  • 51单片机的水位检测系统【proteus仿真+程序+报告+原理图+演示视频】
  • 继电器原理及应用
  • ubuntu2204操作系统使用可执行文件方式安装docker-compose记录
  • 【动态规划-最长公共子序列(LCS)】【hard】【科大讯飞笔试最后一题】力扣115. 不同的子序列