zoukankan      html  css  js  c++  java
  • 700. 二叉搜索树中的搜索

    地址:https://leetcode-cn.com/problems/search-in-a-binary-search-tree/

    <?php
    /**
    700. 二叉搜索树中的搜索
    给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。
    
    例如,
    
    给定二叉搜索树:
    
    4
    / 
    2   7
    / 
    1   3
    
    和值: 2
    你应该返回如下子树:
    
    2
    / 
    1   3
    在上述示例中,如果要找的值是 5,但因为没有节点值为 5,我们应该返回 NULL。
     */
    /**
     * 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
         * @param Integer $val
         * @return TreeNode
         */
        function searchBST($root, $val) {
            if($root == null || $root->val == $val) return $root;
            return $root->val >$val ?$this->searchBST($root->left,$val) :$this->searchBST($root->right,$val);
    
        }
    }
  • 相关阅读:
    webpack
    npm
    关于js click事件、touch事件的 screen 、client
    同源策略、jsonp、阻塞事件
    关于height、width、top
    新建空白图片
    配置环境
    异常02
    异常01
    集合框架08
  • 原文地址:https://www.cnblogs.com/8013-cmf/p/12931626.html
Copyright © 2011-2022 走看看