Skip to main content

Full Stack Developer Without Degree: How to Become One

If you're interested in becoming a Full Stack Developer without a degree, we've put together some tips and resources to help you get started. Learn the Fundamentals The first step to becoming a Full Stack Developer is to learn the fundamentals of coding. You can start with HTML, CSS, and JavaScript, which are the building blocks of web development. There are plenty of free resources available online, such as Codecademy and FreeCodeCamp, which offer interactive courses that teach you the basics of web development. Once you have a solid understanding of the basics, you can move on to more advanced topics such as back-end development, databases, and frameworks. You can learn these topics through online courses or by working on personal projects. Build a Portfolio One of the most important things you can do as a Full Stack Developer is to build a portfolio. Your portfolio should showcase your skills and experience and demonstrate your ability to build real-world applications. You c

Check for BST (DSA)



Problem Statement:(👈click here for GFG)

Given the root of a binary tree. Check whether it is a BST or not.

Note: We are considering that BSTs can not contain duplicate Nodes.
BST is defined as follows:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than the node's key.
  • Both the left and right subtrees must also be binary search trees.

 

Example 1:

Input:
   2
 /    \
1      3
Output: 1 
Explanation: 
The left subtree of root node contains node
with key lesser than the root nodes key and 
the right subtree of root node contains node 
with key greater than the root nodes key.
Hence, the tree is a BST.

Example 2:

Input:
  2
   \
    7
     \
      6
       \
        5
         \
          9
           \
            2
             \
              6
Output: 0 
Explanation: 
Since the node with value 7 has right subtree 
nodes with keys less than 7, this is not a BST.

Your Task:
You don't need to read input or print anything. Your task is to complete the function 
isBST() which takes the root of the tree as a parameter and returns true if the given binary tree is BST, else returns false

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the BST).

Constraints:
0 <= Number of edges <= 100000

Solution:

Approach:

The given code checks whether a binary tree is a Binary Search Tree (BST) or not. The function isBST(root) takes the root of the binary tree as input and returns a boolean value indicating whether it is a valid BST or not. The function isBSTUtil(root, min, max) is a helper function that performs the actual check.

The helper function isBSTUtil() takes three arguments: root, min, and max. root is the current node being processed, and min and max are the minimum and maximum values that a node's data can take to satisfy the BST property. Initially, min is set to -Infinity, and max is set to Infinity. This is because there are no bounds on the data of the root node.

The isBSTUtil() function then checks if the current node violates the BST property. If the current node's data is less than or equal to min or greater than or equal to max, it violates the BST property, and the function returns false.

If the current node does not violate the BST property, the function recursively checks the left and right subtrees. For the left subtree, the min value remains the same, but the max value is updated to the current node's data, as all nodes in the left subtree must have values less than the current node. Similarly, for the right subtree, the max value remains the same, but the min value is updated to the current node's data, as all nodes in the right subtree must have values greater than the current node.

Finally, if both the left and right subtrees are valid BSTs, the function returns true.

function isBST(root) {
    //initially max and min values can be considered as Infinity and -Infinity.
    return isBSTUtil(root, -Infinity, Infinity);
}

function isBSTUtil(root, min, max) {
    //base case.
    if (root === null)
        return true;

    //checking if the current node violates the BST property.
    if (root.data <= min || root.data >= max)
        return false;

    //recursively checking for left and right subtrees.
    return isBSTUtil(root.left, min, root.data) && 
        isBSTUtil(root.right, root.data, max);
}

Another snipet:

class Solution 
{
    //Function to check whether a Binary Tree is BST or not.
    isBST(root)
    {
        //your code here
         //initially max and min values can be considered as Infinity and -Infinity.
        return this.isBSTUtil(root, -Infinity, Infinity);
    }
     isBSTUtil(root, min, max) {
        //base case.
        if (root === null)
            return true;

        //checking if the current node violates the BST property.
        if (root.data <= min || root.data >= max)
            return false;

        //recursively checking for left and right subtrees.
        return this.isBSTUtil(root.left, min, root.data) && 
            this.isBSTUtil(root.right, root.data, max);
    }
}

Thank you,

Learning is a never-ending process, and every day is an opportunity to learn something new. Keep exploring, keep pushing your limits, and keep striving towards your goals. Remember that every small step counts, and even the smallest progress is worth celebrating. Don't be afraid to ask questions, seek guidance, and learn from others. With dedication and hard work, you can achieve anything you set your mind to. So keep learning, keep growing, and keep shining!

Comments

Popular posts from this blog

JavaScript For Loops: A Complete Tutorial

A for loop in JavaScript is a control structure that allows you to repeat a block of code a specified number of times. It is one of the most commonly used loops in JavaScript and is often used when you need to iterate over an array or perform some action a fixed number of times. The syntax for a for loop in JavaScript is as follows: for (initialization; condition; increment/decrement) {    // code to be executed } Here's a breakdown of what each part of the for loop does: Initialization: This is where you initialize the loop variable. It's only executed once before the loop starts. Condition: This is the condition that's checked before each iteration of the loop. If the condition is true, the loop continues. If it's false, the loop ends. Increment/decrement: This is where you change the value of the loop variable after each iteration of the loop. It can be an increment (++) or a decrement (--). Code to be executed: This is the code that's executed for each iteration

Mastering JavaScript Functions: A Complete Tutorial for Beginners

The Ultimate Guide to JavaScript Functions: Everything You Need to Know A function in JavaScript is a block of code that performs a specific task. It can take inputs, process them, and return a result. Functions are reusable, which means that you can call them multiple times with different input values to get different results. Defining a Function To define a function in JavaScript, you can use the function keyword followed by the name of the function, a set of parentheses, and a set of curly braces. Inside the curly braces, you write the code that the function will execute. example: function greet(name) {   console.log("Hello, " + name + "!"); } This function is named greet, and it takes one parameter, name. When you call this function with a string argument, it will print "Hello, [name]!" to the console. Calling a Function To call a function in JavaScript, you simply write the function name followed by a set of parentheses. If the function takes any para

Mastering Variable Data Types and Operations: Essential Tips for Efficient Programming

Mastering Variable Data Types and Operations: A Comprehensive Guide JavaScript is a popular programming language used for web development. Understanding variable data types and operations is essential to master the language. In this article, we will dive into the details of variable data types and operations in JavaScript. Variable Data Types: Variables are used to store values in JavaScript. JavaScript has six different data types that can be stored in a variable. They are as follows: Undefined: A variable is undefined when it is declared but not assigned any value. Null: A variable with a value of null means that it has no value. Boolean: A boolean variable can have a value of true or false. Number: A variable of the number data type can store a numerical value. String : A variable of the string data type can store a sequence of characters. Object: A variable of the object data type can store a collection of key-value pairs. Symbol:  the symbol is a primitive data type that repr