Construct Binary Tree from Inorder and Preorder Traversal

Problem Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree. Examples Example 1: 3 / \ 9 20 / \ 15 7 ...

Merge K Sorted Lists

Problem You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. Examples 1 -> 10 -> 20 4 -> 11 -> 13 3 -> 8 -> 9 will result in 1 -> 3 -> 4 -> 8 -> 9 -> 10 -> 11 -> 13 -> 20 ...

Populate next pointer to inorder successor in Binary Tree

Problem Given a Binary tree where each node has an extra field (node * next), write a function that puts the inorder successor of that node in the next pointer. struct node { int val; struct node * left; struct node * right; struct node * next; } ...

Populate next pointer to right node in Binary Tree

Provide the Next Siblings Pointers in a Given Binary Tree Problem Given a binary tree struct Node { int val; Node *left; Node *right; Node *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. ...

Serialize and Deserialize Binary Tree

Problem Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment. Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure. ...

Inorder Successor in Binary Tree

Problem Find Inorder Successor in Binary Tree. OR Given a Binary tree, find the inorder successor of a node. Definition See the definition here. Examples Example 1: 10 / \ 5 30 / \ 22 35 ...

Print Binary Tree Problem

Problem Given the root of a binary tree, construct a 0-indexed m x n string matrix res that represents a formatted layout of the tree. The formatted layout matrix should be constructed using the following rules: The height of the tree is height and the number of rows m should be equal to height + 1. The number of columns n should be equal to 2height+1 - 1. Place the root node in the middle of the top row (more formally, at location res[0][(n-1)/2]). For each node that has been placed in the matrix at position res[r][c], place its left child at res[r+1][c-2height-r-1] and its right child at res[r+1][c+2height-r-1]. Continue this process until all the nodes in the tree have been placed. Any empty cells should contain the empty string "". Return the constructed matrix res. ...

Number of Good Leaf Nodes Pairs Problem

Problem You are given the root of a binary tree and an integer distance. A pair of two different leaf nodes of a binary tree is said to be good if the length of the shortest path between them is less than or equal to distance. Return the number of good leaf node pairs in the tree. Examples Example 1: 1 / \ 2 3 \ 4 ...

Vertical Order Traversal of a Binary Tree Problem

Vertical Order Traversal of a Binary Tree Problem Problem Given the root of a binary tree, calculate the vertical order traversal of the binary tree. For each node at position (row, col), its left and right children will be at positions (row + 1, col - 1) and (row + 1, col + 1) respectively. The root of the tree is at (0, 0). The vertical order traversal of a binary tree is a list of top-to-bottom orderings for each column index starting from the leftmost column and ending on the rightmost column. There may be multiple nodes in the same row and same column. In such a case, sort these nodes by their values. ...

Find Vertical Sum in binary Tree

Problem Given a binary tree, print it in vertical order sum Examples Example 1: ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy