반응형
https://leetcode.com/problems/search-in-a-binary-search-tree/submissions/
이진 트리에서 노드 찾는 함수를 작성하는 문제였습니다.
재귀를 이용하여 풀었습니다.
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(!root || root->val == val) return root;
return val < root->val ? searchBST(root->left, val) : searchBST(root->right, val);
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 448 : Find All Numbers Disappeared in an Array (0) | 2022.04.16 |
---|---|
LeetCode 538 : Convert BST to Greater Tree (0) | 2022.04.16 |
LeetCode 59 : Spiral Matrix II (0) | 2022.04.13 |
LeetCode 289 : Game of Life (0) | 2022.04.12 |
LeetCode 682 : Baseball Game (0) | 2022.04.10 |