Java DFS in just 0 ms | 2 solutions
Complexity\n- Time complexity: O(N)\n\n# Solution 1: 1 ms | 10% faster\n\nclass Solution {\n    \n    TreeNode result;\n    \n    public TreeNode searchBST(Tree
# Complexity\n- Time complexity: O(N)\n\n# Solution 1: 1 ms | 10% faster\n```\nclass Solution {\n    \n    TreeNode result;\n    \n    public TreeNode searchBST(TreeNode root, int val) {\n        result = null;\n        searchInBST(root, val);\n        return result;\n    }\n\n    private void searchInBST(TreeNode node, int val) {\n        if (node == null) return;\n\n        if (val == node.val) {\n            result = node;\n            return;\n        }\n\n        searchInBST(node.left, val);\n        searchInBST(node.right, val);\n    }\n}\n```\n\n# Solution 2: 0 ms | 100% faster\n```\nclass Solution {\n    public TreeNode searchBST(TreeNode root, int val) {\n        if (root == null) return null;\n        if (root.val == val) return root;\n        if (root.left == null && root.right == null) return null;\n        if (root.left != null && root.left.val == val) return root.left;\n        if (root.right != null && root.right.val == val) return root.right;\n\n        TreeNode result = searchBST(root.left, val);\n        if (result != null) {\n            return result;\n        } else {\n            return searchBST(root.right, val);\n        }\n    }\n}\n```
['Java']