반응형

https://leetcode.com/problems/search-in-a-binary-search-tree/submissions/

 

Search in a Binary Search Tree - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

이진 트리에서 노드 찾는 함수를 작성하는 문제였습니다.

재귀를 이용하여 풀었습니다.

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);
    }
};

 

반응형

+ Recent posts