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);
        }
    }
  • 相关阅读:
    信息系统项目管理师沟通的四个好习惯
    Android 线程
    替换exe程序图标DLL
    Python 邮件类
    android自适应屏幕方向和大小
    sqlserver 存储过程
    FinalData 数据恢复工具[绿色版]
    Python Python 正则 取中括号值
    在Button任意位置加图片效果
    android GPRS
  • 原文地址:https://www.cnblogs.com/CloudStrife/p/7338389.html
Copyright © 2011-2022 走看看