← Back to list

[Leetcode] 700. Search in a Binary Search Tree

You are given the root of a binary search tree (BST) and an integer val.

Jason Xiao · 2022-04-18 13:29 · 0 claps · 0.9 min read
#leetcode #leetcode-medium #leetcode-solution
Open on Medium ↗

[Leetcode] 700. Search in a Binary Search Tree

You are given the root of a binary search tree (BST) and an integer val.

Find the node in the BST that the node’s value equals val and return the subtree rooted with that node. If such a node does not exist, return null.

Example 1:

Input: root = [4,2,7,1,3], val = 2
Output: [2,1,3]

Example 2:

Input: root = [4,2,7,1,3], val = 5
Output: []

Constraints:

  • The number of nodes in the tree is in the range [1, 5000].
  • 1 <= Node.val <= 107
  • root is a binary search tree.
  • 1 <= val <= 107

Ans

[embed]

Solution

Binary Search Tree (BST),中文是 二元搜尋樹

相信有學過任何 計算機概論 / 資料結構 應該都不陌生

實際上二元樹 sample code 網路超級多範例

這邊隨便提供一個 範例

[embed]

相信大家就可以完全理解

如果有不懂可以下面留言一起討論


메타데이터
post_id
a72b490b6597
slug
leetcode-700-search-in-a-binary-search-tree-a72b490b6597
url
https://medium.com/@kosjason/leetcode-700-search-in-a-binary-search-tree-a72b490b6597
canonical_url
https://medium.com/@kosjason/leetcode-700-search-in-a-binary-search-tree-a72b490b6597
author_url
https://medium.com/@kosjason
status
ok
fetched_at
2026-07-27 06:20:56