DDSA
Advertisement

700. Search in a Binary Search Tree

700.cs
C#
public class TreeNode
{
    public int val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null)
    {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution
{
    public TreeNode SearchBST(TreeNode root, int val)
    {
        if (root == null)
            return null;
        if (root.val == val)
            return root;
        if (root.val > val)
            return SearchBST(root.left, val);
        return SearchBST(root.right, val);
    }
}
Advertisement
Was this solution helpful?