"The height of a binary tree is the maximum number of edges from the root node to any leaf node. To calculate the height of a binary tree, we can use a recursive approach. The basic idea is to compare the heights of the left and right subtrees of the root node, and return the maximum of them plus one."
Prashant Y. - "The height of a binary tree is the maximum number of edges from the root node to any leaf node. To calculate the height of a binary tree, we can use a recursive approach. The basic idea is to compare the heights of the left and right subtrees of the root node, and return the maximum of them plus one."See full answer
"class Node
{
int val;
Node left, right;
Node(int v)
{
val = v;
left = right = null;
}
}
class BinaryTree
{
Node root1, root2;
boolean identicalTrees(Node a, Node b)
{
if (a == null && b == null)
return true;
if (a != null && b != null)
return (a.val == b.val
&& identicalTrees(a.left, b.left)
&& identicalTrees(a.right, b.right));
"
Tushar A. - "class Node
{
int val;
Node left, right;
Node(int v)
{
val = v;
left = right = null;
}
}
class BinaryTree
{
Node root1, root2;
boolean identicalTrees(Node a, Node b)
{
if (a == null && b == null)
return true;
if (a != null && b != null)
return (a.val == b.val
&& identicalTrees(a.left, b.left)
&& identicalTrees(a.right, b.right));
"See full answer
"package mycoding.tryexponent.com;
import java.util.Scanner;
public class FirstAndLastOccurenceOfaNumber
{
public static void main(String[] arg)
{
System.out.println("Enter 'a' to run the programme:");
System.out.println("Enter 't' to terminate the programme:");
Scanner in=new Scanner(System.in);
String inputtedString=in.nextLine();
while(!"t".equalsIgnoreCase(inputtedString))
{
System.out.println("enter a number to find its 1st nd last occurence");
int number=in.nextInt();
int[] array=new i"
Ankur T. - "package mycoding.tryexponent.com;
import java.util.Scanner;
public class FirstAndLastOccurenceOfaNumber
{
public static void main(String[] arg)
{
System.out.println("Enter 'a' to run the programme:");
System.out.println("Enter 't' to terminate the programme:");
Scanner in=new Scanner(System.in);
String inputtedString=in.nextLine();
while(!"t".equalsIgnoreCase(inputtedString))
{
System.out.println("enter a number to find its 1st nd last occurence");
int number=in.nextInt();
int[] array=new i"See full answer
"Binary search is commonly used for searching elements in a sorted array. Most searching algorithms take O(n) time, but binary search operates in O(log(n)) time complexity.
function binarySearch(arr, target) {
let first = 0;
let last = arr.length - 1; // Adjusted to correctly represent the last index
while (first target) {
last = mid - 1;
}
"
Satyam S. - "Binary search is commonly used for searching elements in a sorted array. Most searching algorithms take O(n) time, but binary search operates in O(log(n)) time complexity.
function binarySearch(arr, target) {
let first = 0;
let last = arr.length - 1; // Adjusted to correctly represent the last index
while (first target) {
last = mid - 1;
}
"See full answer
"Steps:
Validate K to be less than the number of elements in an array.
Sort the array in ascending order.
Get the K'th smallest element by array[k-1]."
Ashesh S. - "Steps:
Validate K to be less than the number of elements in an array.
Sort the array in ascending order.
Get the K'th smallest element by array[k-1]."See full answer
"bool isConsecutive(int arr[], int n)
{
// base case
if (n max) {
max = arr[i];
}
}
// for an array to contain consecutive integers, the difference between
// the maximum and minimum element in it should be exactly \n-1\
if (max - min != n - 1) {
return false;
}
// create an empty set (we can also use a visit"
Hinata T. - "bool isConsecutive(int arr[], int n)
{
// base case
if (n max) {
max = arr[i];
}
}
// for an array to contain consecutive integers, the difference between
// the maximum and minimum element in it should be exactly \n-1\
if (max - min != n - 1) {
return false;
}
// create an empty set (we can also use a visit"See full answer