NowCder

<?php
header("content-type:text/html;charset=utf-8");
/*
 * 请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。 P159
 */
class TreeNode{
    var $val;
    var $left = NULL;
    var $right = NULL;
    function __construct($val){
        $this->val = $val;
    }
}
function isSymmetrical($pRoot)
{
    if($pRoot == null){
        return null;
    }

    return isSymmetrical2($pRoot,$pRoot);


}

function isSymmetrical2($pRoot1,$pRoot2){
    if($pRoot1->left == null && $pRoot2->right == null){
        return true;
    }
    if($pRoot1->left == null || $pRoot2->right == null){
        return true;
    }
    if($pRoot1->val != $pRoot2->val){
        return false;
    }
    return isSymmetrical2($pRoot1->left,$pRoot2->right) && isSymmetrical2($pRoot1->right,$pRoot2->left);
}

 

相关文章:

  • 2021-09-19
  • 2021-10-16
  • 2021-05-31
  • 2021-12-01
  • 2021-05-24
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-15
  • 2021-12-25
  • 2021-11-22
  • 2022-12-23
相关资源
相似解决方案