美文网首页
LeetCode 第700题:二叉搜索树中的搜索

LeetCode 第700题:二叉搜索树中的搜索

作者: 放开那个BUG | 来源:发表于2020-10-18 14:45 被阅读0次

1、前言

题目描述

2、思路

套用二叉搜索树的套路。

3、代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        if(root == null || root.val == val){
            return root;
        }

        if(root.val > val){
            return searchBST(root.left, val);
        }else{
            return searchBST(root.right, val);
        }
    }
}

相关文章

网友评论

      本文标题:LeetCode 第700题:二叉搜索树中的搜索

      本文链接:https://www.haomeiwen.com/subject/nddorktx.html