Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1 ) Count nodes Given a binary tree root, a node X is named max node if in the path from root to X there
Count nodes
Given a binary tree root, a node X is named max node if in the path from root to X there are no nodes with a value greater than X
Return the number of max nodes in the binary tree.
Definition for a binary tree node.
public class TreeNode
int val;
TreeNode left;
TreeNode right;
TreeNode
TreeNodeint valthisval val;
TreeNodeint val, TreeNode left, TreeNode right
this.val val;
this.left left;
this.right right;
class Solution
public int maxNodesTreeNode root
EncodeDecode a Binary Tree
Design an algorithm to encode turn it into a String and decode read it from String a binary search tree. There is no restriction on how your algorithm should work. You need to ensure that a binary search tree can be encoded to a string, and this string can be decoded to the original tree structure.
Definition for a binary tree node.
public class TreeNode
int val;
TreeNode left;
TreeNode right;
TreeNodeint x val x;
import java.util.;
public class Solution
Encodes a tree to a single string.
public static String encode TreeNode root
Decodes your encoded data to tree.
public static TreeNode decodeString data
String tree Solution.encoderoot;
TreeNode ans Solution.decodetree;
Binary Search Tree Iterator
Apply the Iterator pattern over the Binary Search Tree implementation you have worked on
The iterator should represent an inorder traversal of the BST
Trim Binary Search Tree
Given the root of a binary search tree and the lowest and highest boundaries as low and high, trim the tree so that all its elements lies in low high Trimming the tree should not change the relative structure of the elements that will remain in the tree ie any node's descendant should remain a descendant It can be proven that there is a unique answer.
Return the root of the trimmed binary search tree. Note that the root may change depending on the given bounds.
Example :
Input: root low high
Output: null,
Example :
Input: root null, null, null, low high
Output: null,
Topmost frequent integers
Given an integer array nums and an integer k return the k most frequent integers. Return the answer in any order.
It is guaranteed that the answer is unique.
Your algorithm's time complexity must be better than On log n where n is the array's size basically don't sort anything
Example :
Input: nums k Output:
Example :
Input: nums k Output:
Majority Elements
A Given an integer array of size n find all elements that appear more than Math.floorn times. Solve it in linear time using hash tables.
Example :
Input: nums
Output:
Example :
Input: nums
Output:
Example :
Input: nums
Output:
public List majorityElementint nums
B Solve the same problem as in A but in O space. Consider using the BoyerMoore majority vote algorithm that finds the majority of a sequence of elements using linear time and a constant space.
Arabic to Roman numeral
Given an integer, convert it to a Roman numeral.
public String intToRomanint num
Sort List
Given the head of a singly linked list, sort the list using insertion sort, and return the sorted list's head.
Definition for singlylinked list.
public class ListNode
int val;
ListNode next;
ListNode
ListNodeint valthisval val;
ListNodeint val, ListNode next this.val val; this.next next;
class Solution
public ListNode sortListListNode head
Sort Persons in Linear Time
Create a class Person with two properties: fullName as String and age as Integer. Knowing that the age of a person can't be greater than sort in descending order an array of Persons according to their age. Solve the problem in linear time complexity. Generate an array of Persons using random values unless you want to manually create them
public Person sortPersons Person persons
Heap Sort
Implement heap sort algorithm.
Generate arrays of different sizes and compare the running time of heap sort with other sorting algorithms such as insertion sort, merge sort, quick sort, etc.
Clone Graph
Given the reference of a node in a connected undirected graph, return a deep copy of the graph.
The graph is an adjacency list representation. Each node in the graph contains an integer value which is unique for each node, and a list List of its neighbors. There are no repeated edges and no self
loops in the graph.
The graph is connected and all nodes can be visited starting from the given node. The emphasis is on the deep copy of the graph, don't return the same reference you
are given.
Use the graph Node definition below or one of the Node definitions you have worked on class.
Definition for a Graph Node.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started