700. Search in a Binary Search Tree (recursive)

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* searchBST(TreeNode* root, int val) { if (root == nullptr) return nullptr; else if (root->val == val) return root; else{ TreeNode *leftres = searchBST(root->left, val); TreeNode *rightres = searchBST(root->right, val); if (leftres != nullptr) return leftres; else if (rightres != nullptr) return rightres; else return nullptr; } } };

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.