Question

In: Computer Science

Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary...

Code using JAVA:

must include "Main Method" for IDE testing!

/**
* Definition for a binary tree node.
* 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* sortedArrayToBST(vector<int>& nums) {
  
}
};

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example:

Given the sorted array: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

      0
     / \
   -3   9
   /   /
 -10  5

Solutions

Expert Solution

Java code

class TreeNode {
    
    int val;
    TreeNode left, right;
    
    TreeNode(int d) {
        val = d;
        left = null;
        right = null;
    }
}

public class Solution {
  
    public static TreeNode sortedArrayToBST(int sortedArray[], int left, int right) {

        if (left > right) return null;
       
        int mid = (left + right) / 2;
        TreeNode node = new TreeNode(sortedArray[mid]);

        node.left = sortedArrayToBST(sortedArray, left, mid - 1);

        node.right = sortedArrayToBST(sortedArray, mid + 1, right);
        
        return node;
    }

    public static void inOrder(TreeNode node) {
        if (node == null) return;
        inOrder(node.left);
        System.out.print(node.val + " ");
        inOrder(node.right);
    }
    
    public static void main(String[] args) {
        int sortedArray[] = new int[]{-10,-3,0,5,9};
        int n = sortedArray.length;
        TreeNode root = sortedArrayToBST(sortedArray, 0, n - 1);
        System.out.println("Inorder traversal");
        inOrder(root);
    }
}


Related Solutions

Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public TreeNode searchBST(TreeNode root, int val) {    }...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary tree node. * 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: vector<int> inorderTraversal(TreeNode* root) {    } }; Given the root of a binary tree, return...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary tree node. * 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: vector<int> preorderTraversal(TreeNode* root) {    } }; Given the root of a binary tree, return...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use a hash...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use a hash table. You can use either Java HashTable or Java HashMap. Refer to Java API for the subtle differences between HashTable and HashMap. Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each column must contain the digits 1-9 without repetition. Each...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use recursion "...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use recursion " /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public boolean isSymmetric(TreeNode...
I am using NetBeans IDE Java to code and I am seeking comment to the code...
I am using NetBeans IDE Java to code and I am seeking comment to the code as well (for better understanding). Magic squares. An n x n matrix that is filled with the numbers 1, 2, 3, …, n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. Write a program that reads in 16 values from the keyboard, displays them in a 4...
Write code using the Arduino IDE that compiles with no errors. 2-bit adder: The code must...
Write code using the Arduino IDE that compiles with no errors. 2-bit adder: The code must read two digital input signals and turn on two LEDS as needed to show the sum of the inputs. e.g. 0 + 1 = 01.
This code must be written in Java. This code also needs to include a package and...
This code must be written in Java. This code also needs to include a package and a public static void main(String[] args) Write a program named DayOfWeek that computes the day of the week for any date entered by the user. The user will be prompted to enter a month, day, and year. The program will then display the day of the week (Sunday..Saturday). The following example shows what the user will see on the screen: This program calculates the...
JAVA CODE Define a method called changeGroupPrice that could be added to the definition of the...
JAVA CODE Define a method called changeGroupPrice that could be added to the definition of the class Purchase. This method has one parameter that is of type double, and is named salePercent. This number represents the percent reduction in the groupPrice amount.   The method uses this number to change the groupPrice. The code of the method should check to make sure the range of salePercent is between 0 and 50%. If it is, the groupPrice should be adjusted accordingly. If...
I am using NetBeans IDE Java for coding. I would like the code to be commented...
I am using NetBeans IDE Java for coding. I would like the code to be commented for a better understanding. 1. Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces north, east, south, or west. Supply methods: public void turnLeft() public void turnRight() public void move() public Point getLocation() public String getDirection() The turnLeft and turnRight methods change the direction but not the location....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT