【PHP解法==LeetCode(二叉树中的递归3-验证)】101.对称二叉树 && 110.平衡二叉树 && 98. 验证二叉搜索树

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010365335/article/details/87876910

目录

101.对称二叉树

110.平衡二叉树

98. 验证二叉搜索树


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

解法:

(1)判断两个比较的节点是否为空,空则true;仅有一个空则false

(2)判断两个比较的节点的值是否为相同,相同则继续递归遍历

(3)分别递归(1节点的左孩子,2节点的右孩子)和(1节点的右孩子,2节点的左孩子),达到镜像对称判断的目的

/**
 * 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 {
    function isSymmetric($root) {
        return $this->isMirror($root,$root);    //主递归函数,判断是否镜像对称
    }
    public function isMirror($p,$q){
        if($p == null && $q == null) return true;   //该节点为空,则一样,返回true
        if($p == null || $q == null) return false;  //仅有一个节点为空,则肯定不一样,返回false
        //判断两个节点的值是否相同,如果相同,则继续递归
        //分别递归第一个节点的左孩子和第二个节点的右孩子,进行比较
        //递归第一个节点的右孩子和第二个节点的左孩子,进行比较
        return ($p->val == $q->val) && $this->isMirror($p->left,$q->right) && $this->isMirror($p->right,$q->left);
    }
}

LeetCode100.相同的树,递归依次比较左右子树


110.平衡二叉树

给定一个二叉树,判断它是否是高度平衡的二叉树。

本题中,一棵高度平衡二叉树定义为:

一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。

示例 1:

给定二叉树 [3,9,20,null,null,15,7]

    3
   / \
  9  20
    /  \
   15   7

返回 true 。

示例 2:

给定二叉树 [1,2,2,3,3,null,null,4,4]

       1
      / \
     2   2
    / \
   3   3
  / \
 4   4

返回 false 。

解法:依次判断一个节点的左右高度是否相差不超过1

class Solution {	
    function isBalanced($root) {
        if($root == null) return true;      //树为空,则默认为平衡二叉树
        //左右子树的高度的差的绝对值大于1,则不满足条件
        if(abs($this->maxDepth($root->left) - $this->maxDepth($root->right)) > 1)
            return false;
        //满足,则剔除该节点,并继续左右节点的遍历判断
        if($this->isBalanced($root->left) && $this->isBalanced($root->right))
            return true;
        return false;
    }
    /**
     * 取该节点往下的最大深度
     */
    public function maxDepth($root) {
        if($root == null)
            return 0;
        $leftMaxLength = $this->maxDepth($root->left);
        $rightMaxLength = $this->maxDepth($root->right);
        return max($leftMaxLength,$rightMaxLength)+1;
    }
}

98. 验证二叉搜索树

给定一个二叉树,判断其是否是一个有效的二叉搜索树。

假设一个二叉搜索树具有如下特征:

  • 节点的左子树只包含小于当前节点的数。
  • 节点的右子树只包含大于当前节点的数。
  • 所有左子树和右子树自身必须也是二叉搜索树。

示例 1:

输入:
    2
   / \
  1   3
输出: true

示例 2:

输入:
    5
   / \
  1   4
     / \
    3   6
输出: false
解释: 输入为: [5,1,4,null,null,3,6]。
     根节点的值为 5 ,但是其右子节点值为 4 。

二叉搜索树的条件,左子树的所有节点比根结点的小,右子树的所有节点比根结点的大

因此问题可以转换成二叉树的中序遍历结果为升序

class Solution {
    public $res = [];           //存储中序遍历的结果
    function isValidBST($root) {
        //中序遍历为升序即可
        if($root == null) return true;
        $this->inOrder($root);       //进行中序遍历
        $len = count($this->res);
        $pre = $this->res[0];
        //比较是否升序
        for($i = 1;$i<$len;++$i){
            $cur = $this->res[$i];
            if($cur <= $pre)
                return false;
            $pre = $cur;
        }
        return true;
    }
    public function inOrder($root){
        if($root){
            $this->inOrder($root->left);
            $this->res[] = $root->val;
            $this->inOrder($root->right);
        }
    }
}

LeetCode230.二叉搜索树的第K小元素:同理求中序遍历压入结果的第K个元素即可

猜你喜欢

转载自blog.csdn.net/u010365335/article/details/87876910