タイトルアドレス:
https://www.lintcode.com/problem/search-in-a-binary-search-tree/description
BSTと数値ターゲットを指定して、値がターゲットと等しいノードを見つけます。コードは次のとおりです。
public class Solution {
/**
* @param root: the tree
* @param val: the val which should be find
* @return: the node
*/
public TreeNode searchBST(TreeNode root, int val) {
// Write your code here.
while (root != null && root.val != val) {
root = root.val < val ? root.right : root.left;
}
return root;
}
}
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int x) {
val = x;
}
}
時間の複雑さ 。