0% found this document useful (0 votes)
9 views3 pages

Binary Tree Miscellaneous

The document discusses several algorithms and data structures including flattening a binary tree to a linked list, finding the median of a data stream, finding the kth largest element in a stream/array, checking if a tree is a subtree of another tree, converting a binary tree to a doubly linked list using inorder traversal, and calculating distinct numbers in a sliding window.

Uploaded by

Aastha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Binary Tree Miscellaneous

The document discusses several algorithms and data structures including flattening a binary tree to a linked list, finding the median of a data stream, finding the kth largest element in a stream/array, checking if a tree is a subtree of another tree, converting a binary tree to a doubly linked list using inorder traversal, and calculating distinct numbers in a sliding window.

Uploaded by

Aastha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

- Flatten Binary Tree To Linked List

- Method 1: Find preorder; save it; create list based on that preorder
- Method 2: Use modified Morris preorder traversal to optimise/ have constant space

Idea is to attach the predecessor to the parent’s right WHICH MEANS attaching the last
node explored in left subtree to the first node explored in right subtree(as per preorder
traversal)

- Find median from data stream

- Method 1: Save the numbers in a list as they come; and recompute the median each
time the findMedian() function is called.
Recomputation involves, 1. Sort the list, 2. According to the number of elements, finding
the median index/ avg of middle values(if even)

Time complexity of findMedian(): O(nlogn) + O(1)[assuming that a size variable is


maintained throughout and calculating the median]

- Method 2: Keeping track of middle elements of the stream as the data is added each
time, it would optimise the solution.
Maintain a max heap for the left half of the data, and a min heap for the right half of the
data. The max heap stores the maximum element in the left portion(thus the rightmost
extreme element) and the min heap stores the minimum element in the right portion(thus
the leftmost extreme element)

After each addition of an element, we should balance the heaps such that the difference
between the sizes is not greater than one. Since that would lose the middle element if
the parts were not correctly partitioned.

Time complexity: O(log n) for adding an element +O(log n) for balance function + O(1)
for computing median
Space complexity: O(n) overall since two halves of the data stream is maintained in two
different heaps.

- Kth largest element in a stream:/ Kth largest element in an array: modification(BUT


SIMILAR concept)
Method 1: When adding an element in an array, also sort it in descending order and
return the k-1th index value. (n log n) time complexity and (n) space complexity.
Method 2: Using a heap can store the k element at a certain point, and find out the kth
element. We can use a min heap of size k to identify the kth largest element,
When we are adding the element, we will compare the above conditions only.
This constructor takes n log k. And the add function takes log k time.

Space is O(log K)

- Check If Subtree:
Check if given 2 root nodes, the S tree is a subset of the T tree.

Basically, this involves, first checking the null inequalities


Then we can check whether its the same tree and if not, we can check if s is a subtree of
the left side of the T tree, or the right side.

Time Complexity: O(M*N), Traversing on subtree S of size M for every N


node of Tree T.
The space complexity is O(max(M, N)) due to the recursive calls on the stack, where M
and N are the number of nodes in S and T respectively.

- Find a given binary tree to doubly linked list according to inorder

Inorder morris traversal with slight modifications can implement this algorithm in O(1) space and
O(n) space since we visit the nodes at most two times in this traversal.
Method 2: Can store the inorder traversal, and create a DLL from these values. Extra space
complexity involved here.

Distinct Numbers in a Window:

Given a window size, calculate the number of distinct numbers in all windows of given size.

Brute force approach:


For each window, calculate the distinct elements. Time complexity for this approach: (n-m+1)*m
since n-m+1 are the number of starting indexes are for each starting index, we are computing
the distinct elements by iterating over that window.

Optimal approach:
Instead of recomputing for every window, we can simply use the previous results to simplify the
process. A sliding technique is used here such that when we are moving to the next window, we
remove the previous element from the tracker(a map) and add the new element to it. Then we
can calculate the map size to get the distinct elements.
This approach involves iterating over the array once and additional space depending on the
window size.
If window size is K, the space complexity will be O(K)
And the time complexity will be: O(N) where N is the size of the array.

You might also like