首页 > 其他 > 详细

面试题28. 对称的二叉树

时间:2020-05-20 19:10:38      阅读:51      评论:0      收藏:0      [点我收藏+]

地址:https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/

<?php
/**
101. 对称二叉树
给定一个二叉树,检查它是否是镜像对称的。



例如,二叉树 [1,2,2,3,4,4,3] 是对称的。

1
/ 2   2
/ \ / 3  4 4  3


但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:

1
/ 2   2
\   3    3
 */

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($value) { $this->val = $value; }
 * }
 */
class Solution {

    /**
     * @param TreeNode $root
     * @return Boolean
     */
    function isSymmetric($root) {
        return $this->helper($root,$root);
    }

    function helper($root1,$root2){
        if($root1 == null && $root2 ==null) return true;
        if($root1 == null || $root2 == null) return false;
        if($root1->val != $root2->val) return false;

        $ret = $this->helper($root1->left,$root2->right);
        $ret1 = $this->helper($root1->right,$root2->left);
        return $ret && $ret1;
    }
}

 

面试题28. 对称的二叉树

原文:https://www.cnblogs.com/8013-cmf/p/12924752.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!