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