二分搜索树-二叉树中的搜索(领扣)

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        if(root==null){
            return null;
        }
        if(val<root.val){
             return searchBST(root.left,val);
        }else if(val>root.val){
            return searchBST(root.right,val);  
        }else{
            return root;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43283092/article/details/89536475