算法训练day16 | php | 530.二叉搜索树的最小绝对差 , 501.二叉搜索树中的众数 , 236. 二叉树的最近公共祖先

一、力扣题530. 二叉搜索树的最小绝对差

给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 。

差值是一个正数,其数值等于两值之差的绝对值。

示例 1:

算法训练day16 | php | 530.二叉搜索树的最小绝对差 , 501.二叉搜索树中的众数 , 236. 二叉树的最近公共祖先_第1张图片

输入:root = [4,2,6,1,3]
输出:1

示例 2:

算法训练day16 | php | 530.二叉搜索树的最小绝对差 , 501.二叉搜索树中的众数 , 236. 二叉树的最近公共祖先_第2张图片

输入:root = [1,0,48,null,null,12,49]
输出:1

提示:

  • 树中节点的数目范围是 [2, 104]
  • 0 <= Node.val <= 105

方法一:递归法 

         因为二叉搜索树的特性,把它中序遍历成数组,就是一个升序排列的数组,这样来对比得出最小差值,就变得很简单了。整体逻辑和上一篇里的“验证二叉搜索树"类似,都是当前节点和上一个节点的判断对比。再设置一个全局变量 min,初始值为整型里的最大值。

class Solution {
    private $min = PHP_INT_MAX;
    private $pre = null;
    /**
     * @param TreeNode $root
     * @return Integer
     */
    function getMinimumDifference($root) {
        $this->getMin($root);
        return $this->min;
    }

    function getMin($root) {
        if($root->left) $this->getMinimumDifference($root->left);

        if($this->pre !== null && abs($this->pre - $root->val) < $this->min) {
            $this->min = abs($this->pre - $root->val);
        }
        $this->pre = $root->val;

        if($root->right) $this->getMinimumDifference($root->right);

        return;
    }
}

方法二:迭代法 

        同样,用栈的统一遍历方式来进行中序遍历也能达到相同效果。

function getMinimumDifference($root) {
        $min = PHP_INT_MAX;
        $pre = null;

        $stack = new splStack();
        $stack->push($root);

        while(!$stack->isEmpty()) {
            $cur = $stack->pop();
            if($cur === null) {
                $root = $stack->pop()->val;
                if($pre !== null && abs($pre - $root) < $min) 
                    $min = abs($pre - $root);
                $pre = $root;
            } else {
                if($cur->right) $stack->push($cur->right);

                $stack->push($cur);
                $stack->push(null);

                if($cur->left) $stack->push($cur->left);
            }
        }

        return $min;
    }

二、力扣题501. 二叉搜索树中的众数

给你一个含重复值的二叉搜索树(BST)的根节点 root ,找出并返回 BST 中的所有 众数(即,出现频率最高的元素)。

如果树中有不止一个众数,可以按 任意顺序 返回。

假定 BST 满足如下定义:

  • 结点左子树中所含节点的值 小于等于 当前节点的值
  • 结点右子树中所含节点的值 大于等于 当前节点的值
  • 左子树和右子树都是二叉搜索树

示例 1:

算法训练day16 | php | 530.二叉搜索树的最小绝对差 , 501.二叉搜索树中的众数 , 236. 二叉树的最近公共祖先_第3张图片

输入:root = [1,null,2,2]
输出:[2]

示例 2:

输入:root = [0]
输出:[0]

提示:

  • 树中节点的数目在范围 [1, 104] 内
  • -105 <= Node.val <= 105

 方法一:递归将其转化为数组

        依旧是利用二叉搜索树的特性,使用中序遍历的值就是从小到大的,利用此特性统计出一个map数组,该数组的键名key为元素值,键值value为元素在二叉树中出现的频率,再遍历一遍该map数组获取到频率最高的众数们。

class Solution {
    private $arr = [];
    /**
     * @param TreeNode $root
     * @return Integer[]
     */
    function findMode($root) {
        $this->getArr($root);
        $array = $this->arr;
        $max = 0;
        $res = [];

        foreach($array as $key => $val) {
            if($val == $max) {
                $res[] = $key;
            }
            if($val > $max) {
                $res = [];
                $res[] = $key;
                $max = $val;
            }
        }

        return $res;
    }

    function getArr($root) {
        if($root->left) $this->getArr($root->left);
        if($this->arr[$root->val]) {
            $this->arr[$root->val]++;
        } else {
            $this->arr[$root->val] = 1;
        }
        if($root->right) $this->getArr($root->right);
        return;
    }
}

方法二:直接递归法

        直接中序遍历完成筛选最大频率元素的工作,本身逻辑并不难理解,但细节很容易错。

class Solution {
    private $arr = [];
    private $max = 0;
    private $pre = null;
    private $count = 0;
    /**
     * @param TreeNode $root
     * @return Integer[]
     */
    function findMode($root) {
        $this->getArr($root);
        return $this->arr;
    }

    function getArr($root) {
        if($root->left) $this->getArr($root->left);

        if($this->pre === null) {
            $this->count = 1;
            $this->pre = $root->val;
        } else if($this->pre == $root->val) {
            $this->count++;
        } else {
            $this->count = 1;
            $this->pre = $root->val;
        }

        if($this->count == $this->max) {
            $this->arr[] = $root->val;
        }
        if($this->count > $this->max) {
            $this->max = $this->count;
            $this->arr = [];
            $this->arr[] = $root->val;
        }

        if($root->right) $this->getArr($root->right);
        return;
    }
}

三、力扣题236. 二叉树的最近公共祖先

给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。

百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”

示例 1:

算法训练day16 | php | 530.二叉搜索树的最小绝对差 , 501.二叉搜索树中的众数 , 236. 二叉树的最近公共祖先_第4张图片

输入:root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
输出:3
解释:节点5和节点1的最近公共祖先是节点3 。

示例 2:

算法训练day16 | php | 530.二叉搜索树的最小绝对差 , 501.二叉搜索树中的众数 , 236. 二叉树的最近公共祖先_第5张图片

输入:root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
输出:5
解释:节点 5 和节点 4 的最近公共祖先是节点 5 。因为根据定义最近公共祖先节点可以为节点本身。

示例 3:

输入:root = [1,2], p = 1, q = 2
输出:1

提示:

  • 树中节点数目在范围 [2, 105] 内。
  • -109 <= Node.val <= 109
  • 所有 Node.val 互不相同 。
  • p != q
  • p 和 q 均存在于给定的二叉树中。

         这道题一开始一直想不到怎么解,然后去看了题解,发现有这么一个判断,从上往下遍历,如果一个节点的左子树和右子树中分别有一个指定节点,或者这个节点本身就是指定节点中的一个,那么这个节点就是两个指定节点最近的公共祖先。

        这样一来递归的条件就理清了。

        递归参数:当前节点、两个指定节点;

        终止条件:节点的左子树和右子树中分别有一个指定节点  或   节点本身就是指定节点中的一个;

        返回参数:返回指定节点最近的公共祖先。

        单层递归逻辑:使用前序遍历,先判断中节点是否为指定节点 或 为空,符合直接返回该节点。不符合则递归左右节点,对递归返回的两个参数进行判断,决定返回参数。

function lowestCommonAncestor($root, $p, $q) {
        if($root === null || $root == $p || $root == $q)  return $root;
            
        $left = $this->lowestCommonAncestor($root->left, $p, $q);
        $right = $this->lowestCommonAncestor($root->right, $p, $q);

        if($left && $right) {
            return $root;
        }
        if($left) return $left;
        if($right) return $right;
    }

你可能感兴趣的:(算法,数据结构,leetcode,php)