"Recursively get the left height and right height of the sub-tree, and then check if it is balanced at this node, return false."
Nishant R. - "Recursively get the left height and right height of the sub-tree, and then check if it is balanced at this node, return false."See full answer
"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
Coding
Data Structures & Algorithms
🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.
"function findMinMax(array){
array.sort((a,b) => a - b);
let min = array[0];
let max = array.slice(-1);
return [min,max];
}
`"
Adam S. - "function findMinMax(array){
array.sort((a,b) => a - b);
let min = array[0];
let max = array.slice(-1);
return [min,max];
}
`"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 the technique to search an element in a sorted array in O(log(N)) time. It is also used in problems wherever we find monotonically increasing or decreasing patterns."
Shivam S. - "Binary search is the technique to search an element in a sorted array in O(log(N)) time. It is also used in problems wherever we find monotonically increasing or decreasing patterns."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