LF.52.Search In Binary Search Tree

Find the target key K in the given binary search tree, return the node that contains the key if K is found, otherwise return null.

Assumptions

There are no duplicate keys in the binary search tree
 1 public class Solution {
 2   public TreeNode search(TreeNode root, int key) {
 3     // Write your solution here.
 4     //base case
 5     if (root == null) {
 6         return null ;
 7     }
 8     if (root.key == key) {
 9         return root ;
10     }
11     if (root.key < key ) {
12         return search(root.right, key);
13     }
14     if (root.key > key) {
15         return search(root.left, key) ;
16     }
17     return null;
18   }
19 }

原文地址:https://www.cnblogs.com/davidnyc/p/8655270.html

时间: 2024-10-10 17:36:36

LF.52.Search In Binary Search Tree的相关文章

LF.51.Insert In Binary Search Tree

Insert a key in a binary search tree if the binary search tree does not already contain the key. Return the root of the binary search tree. Assumptions There are no duplicate keys in the binary search tree If the key is already existed in the binary

LF.53.Delete In Binary Search Tree

Delete the target key K in the given binary search tree if the binary search tree contains K. Return the root of the binary search tree. Find your own way to delete the node from the binary search tree, after deletion the binary search tree's propert

12. binary search Trees

12. binary search Trees? ? The search tree data structure supports many dynamic-set operations,including search ,minimum,maximum,predecessor,successor ,insert ,and delete.? Thus, we can use a search tree both as a dictionary and as a priority queue.

Binary Search

Time complexity O(log(n)). When the question requires O(log(n)) time complexity, we always need to think whether it can be solved by binary search. For binary search, there are several key elements to consider: 1. when to stop while loop? 2. how to c

LeetCode99 Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. (Hard) Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? 分析: BST的中序遍历应该是递增的,我们考

PTA Build A Binary Search Tree(C语言)

题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys gre

A1043. Is It a Binary Search Tree (25)

Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater

Binary search tree system and method

A binary search tree is provided for efficiently organizing values for a set of items, even when values are duplicated. In generating the binary search tree, the value of each item in a set of values is determined. If a particular value is unique and

UVA1264 &amp;&amp; UVAlive4847 Binary Search Tree

A binary search tree is a binary tree. It may be empty. If it is not empty, it satisfies the following properties: (1) Every node has a key, and no two nodes have the same key. (2) The keys in a nonempty left subtree must be smaller than the key in t