欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【leetcode】700. Search in a Binary Search Tree

程序员文章站 2022-06-08 09:54:18
...

 

/**
 * 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)
            return null;
        
        if(root.val==val)
            return root;
        
        if(val<root.val)
            return searchBST(root.left, val);
        else
            return searchBST(root.right, val);
    }
}