【Lintcode】1524。バイナリ検索ツリーでの検索

タイトルアドレス:

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;
    }
}

時間の複雑さ h ああ)

公開された387元の記事 ウォンの賞賛0 ビュー10000 +

おすすめ

転載: blog.csdn.net/qq_46105170/article/details/105465251