LeetCode75-700-Search-in-BST

🧠 Summary of Thoughts

A Binary Search Tree is an ordered Binary Tree.

Binary Tree (BT)、Binary Search Tree (BST)、Heap 的核心区别:

Do not utilize the properties of BST (Traverse the entire tree)

This method didn’t use the properties of BST, need to traverse the entire tree, which is slow.

Utilize the properties of BST

Compare the values ($val < root.val$, $val = root.val$, $val > root.val$).

✅ Code(Java)

  1. Do not utilize the properties of BST (Traverse the entire tree)
1
2
3
4
5
6
7
8
9
10
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if (root == null) return null;
if (root.val == val) return root;
TreeNode left = searchBST(root.left, val);
if (left != null) return left;
TreeNode right = searchBST(root.right, val);
return right;
}
}
  1. Utilize the properties of BST
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if (root == null) return null;
if (root.val == val) return root;
if (val < root.val) {
return searchBST(root.left, val);
} else {
return searchBST(root.right, val);
}
}
}

LeetCode75-700-Search-in-BST
https://powersixxx.github.io/leetcode-blog/2026/03/08/LeetCode75-700-Search-in-BST/
作者
Baowen Liu
发布于
2026年3月8日
许可协议