#222

Поделиться
HTML-код
  • Опубликовано: 27 окт 2024

Комментарии • 3

  • @LaxmanSingh-xw5xx
    @LaxmanSingh-xw5xx 3 месяца назад

    Nice ❤

  • @pritamkumarpattanayak1497
    @pritamkumarpattanayak1497 3 месяца назад

    GFG POTD 25/07/2024
    Node *buildBSTFromInorder(const vector &inorder, int start, int end)
    {
    if (start > end)
    {
    return nullptr;
    }
    int mid = (start + end) / 2;
    Node *node = new Node(inorder[mid]);
    node->left = buildBSTFromInorder(inorder, start, mid - 1);
    node->right = buildBSTFromInorder(inorder, mid + 1, end);
    return node;
    }
    class Solution {
    public:
    Node* sortedArrayToBST(vector& nums) {
    // Code here
    Node *root = buildBSTFromInorder(nums, 0, nums.size() - 1);
    return root;
    }
    };

  • @ashwinbalaji26
    @ashwinbalaji26 3 месяца назад

    My solution is also same except I declared prev as class attribute and used same function.
    Code:
    class Solution {
    int prev = -1;
    public:
    bool isBST(Node* root) {
    if(!root) return true;
    bool left = isBST(root->left);
    if(!left) return false;
    if(root->data data;
    bool right = isBST(root->right);
    return left && right;
    }
    };