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

    请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。

    分析:对称二叉树就是相对于中间的根左右两边对称left.left==right.right&&left.right==right.left

    /* function TreeNode(x) {
    
        this.val = x;
    
        this.left = null;
    
        this.right = null;
    
    } */
    
    function isSymmetrical(pRoot)
    
    {
    
        // write code here
    
        if(pRoot===null){
    
            return true
    
        }
    
        return check(pRoot.left,pRoot.right)
    
    }
    
    function check(left,right){
    
        if(left===null){
    
            return right===null
    
        }
    
        if(right===null){
    
            return false
    
        }
    
        if(left.val!==right.val){
    
            return false
    
        }
    
        return check(left.left,right.right)&&check(left.right)
    
    }
  • 相关阅读:
    案例分析
    202103226-1 编程作业
    阅读任务
    准备工作
    结对作业
    案列分析
    202103226-1 编程作业
    《构建之法》有感
    准备工作
    案例分析作业
  • 原文地址:https://www.cnblogs.com/mlebk/p/12632485.html
Copyright © 2011-2022 走看看