zoukankan      html  css  js  c++  java
  • 剑指offer28题

    判断一颗二叉树,是不是对称的。如果一颗二叉树和它的镜像一样,则它是对称的。

    举例如下,该树为对称的。但这个就不是。

    对于这个题目。我第一眼想到的办法是,首先我把一棵树转换成它的镜像,然后通过先序,读取镜像树和原始树的数据,并保存在数组中,或者栈中。,然后对着2个数组,或者栈进行匹配。但耗时比较长。书中给出的答案是实现一种对称序列。具体不多说,直接上代码。

    package com.algorithm04;
    
    import com.tools.TreeBinaryFunction;
    import com.tools.TreeNode;
    
    public class Algorithm28 {
        
        public static boolean Solution(TreeNode root){
            if(root==null){
                return true;
            }
            return Solution(root.left,root.right);
        }
        public static boolean Solution(TreeNode leftNode , TreeNode rightNode){
            if(leftNode == null && rightNode == null){
                return true;
            }
            if(leftNode == null || rightNode == null){
                return false;
            }
            if(leftNode.val == rightNode.val)    
                return Solution(leftNode.left, rightNode.right)&&Solution(leftNode.right, rightNode.left);
            return false;
        }
        
        public static void main(String[] args) {
            TreeNode treeNode = new TreeNode(0);
            TreeBinaryFunction.CreateTreeBinary(treeNode);
            boolean isflag = Solution(treeNode);
            System.err.println(isflag);
        }
    }
  • 相关阅读:
    管理之道
    Swagger-editor安装启动及错误处理,注意事项
    装箱 拆箱 枚举 注解 多态
    Spring Security 内置过滤器表
    Spring Boot入门 and Spring Boot与ActiveMQ整合
    消息中间件解决方案JMS
    网页静态化解决方案-Freemarker demo+语法
    spring-data-radis错误
    java基础总结
    swift oc 混编的头文件
  • 原文地址:https://www.cnblogs.com/CloudStrife/p/7338389.html
Copyright © 2011-2022 走看看