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

700. Search in a Binary Search Tree

程序员文章站 2022-06-08 09:31:36
...

#700. Search in a Binary Search Tree

Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node’s value equals the given value. Return the subtree rooted with that node. If such node doesn’t exist, you should return NULL.

var searchBST = function(root, val) {
    if(root===null) {
        return root;
    }
    else if(root.val===val) {
        return root;
    }
    else if(val<root.val) {
        return searchBST(root.left,val);
    }
    else {
        return searchBST(root.right,val);
    }
};