码迷,mamicode.com
首页 > 其他好文 > 详细

LF.52.Search In Binary Search Tree

时间:2018-03-27 10:22:51      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:arch   ISE   div   target   null   dup   color   int   nbsp   

 

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 }

 

LF.52.Search In Binary Search Tree

标签:arch   ISE   div   target   null   dup   color   int   nbsp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!