zoukankan      html  css  js  c++  java
  • 对称的二叉树

    题目描述

    请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
    /**
     * 
     * @author gentleKay
     * 题目描述
     * 请实现一个函数,用来判断一颗二叉树是不是对称的。
     * 注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
     */
    
    public class Main56 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		TreeNode root = new TreeNode(1);
    		root.left = new TreeNode(2);
    		root.right = new TreeNode(2);
    		
    		root.left.left = new TreeNode(3);
    		root.left.right = new TreeNode(4);
    		
    		root.right.left = new TreeNode(4);
    		root.right.right = new TreeNode(3);
    		
    //		root.left.left.left = new TreeNode(5);
    		
    		System.out.println(Main56.isSymmetrical(root));
    		
    	}
    	
    	public static class TreeNode {
    	    int val = 0;
    	    TreeNode left = null;
    	    TreeNode right = null;
    
    	    public TreeNode(int val) {
    	        this.val = val;
    
    	    }
    	}
    	
        static boolean isSymmetrical(TreeNode pRoot){
        	if (pRoot == null) {
        		return true;
        	}
        	
        	return isSym(pRoot.left, pRoot.right);
        }
        
        static boolean isSym(TreeNode RootLeft, TreeNode RootRight) {
        	if (RootLeft == null && RootRight == null) {
        		return true;
        	}
        	if (RootLeft == null || RootRight == null) {
        		return false;
        	}
        	
        	if (RootLeft.val == RootRight.val) {
        		return isSym(RootLeft.left,RootRight.right) && isSym(RootLeft.right, RootRight.left);
        	}
        	return false;
        }
    }
    

      

  • 相关阅读:
    pthread_key_t和pthread_key_create()详解
    oracle的时间
    spring加载bean报错:expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    MyBatis调用存储过程
    java的时间
    java的日期
    java中的多态
    笔记本设置wifi热点并抓包
    WiresShark使用说明
    HTTP协议概述
  • 原文地址:https://www.cnblogs.com/strive-19970713/p/11202637.html
Copyright © 2011-2022 走看看