Skip to content

Commit

Permalink
LeetCode: 700. Search in a Binary Search Tree
Browse files Browse the repository at this point in the history
- accepted;
  • Loading branch information
olegon committed Aug 17, 2023
1 parent 8a74524 commit 932290d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

set -x

clang++ main.cpp -W -Wall -std=c++17 -O2 -fsanitize=address && ./a.out
39 changes: 39 additions & 0 deletions leetcode/problems/search-in-a-binary-search-tree/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
700. Search in a Binary Search Tree
https://leetcode.com/problems/search-in-a-binary-search-tree/
*/

#include <bits/stdc++.h>

using namespace std;

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
while (root != nullptr && root->val != val) {
if (root->val > val) root = root->left;
else root = root->right;
}

return root;

// if (root == nullptr || root->val == val) return root;
// else if (root->val > val) return searchBST(root->left, val);
// else return searchBST(root->right, val);
}
};

int main(void) {
ios::sync_with_stdio(false);

return EXIT_SUCCESS;
}

0 comments on commit 932290d

Please sign in to comment.