robinsdeepak / leetcode

0 stars 0 forks source link

Day 21: 1. Floor in a BST #144

Closed robinsdeepak closed 2 years ago

robinsdeepak commented 2 years ago

Problem Name: Floor in a BST Problem Link: https://www.codingninjas.com/codestudio/problems/floor-from-bst_920457 C++ Code: https://github.com/striver79/FreeKaTreeSeries/blob/main/L42_floorInBstCpp Detailed Solution: Java Code: https://github.com/striver79/FreeKaTreeSeries/blob/main/L42_floorInBstJava Video Solution: https://www.youtube.com/watch?v=xm_W1ub-K-w&list=PLgUwDviBIf0q8Hkd7bK2Bpryj2xVJk8Vk&index=43

robinsdeepak commented 2 years ago
int floorInBST(TreeNode<int> * root, int X)
{
    TreeNode<int> *ptr = root;
    int ans;
    while (ptr)
    {
        if (ptr->val == X)
            return X;

        if (ptr->val < X)
        {
            ans = ptr->val;
            ptr = ptr->right;
        }
        else
        {
            ptr = ptr->left;
        }
    }
    return ans;
}