Knowee
Questions
Features
Study Tools

Given a Linked List Representation of Complete Binary Tree. Your task is to construct the Binary tree from the given linkedlist and return the root of the tree.The result will be judged by printing the level order traversal of the Binary tree. Note: The complete binary tree is represented as a linked list in a way where if the root node is stored at position i, its left, and right children are stored at position 2*i+1, and 2*i+2 respectively. H is the height of the tree and this space is used implicitly for the recursion stack.

Question

Given a Linked List Representation of Complete Binary Tree. Your task is to construct the Binary tree from the given linkedlist and return the root of the tree.The result will be judged by printing the level order traversal of the Binary tree. Note: The complete binary tree is represented as a linked list in a way where if the root node is stored at position i, its left, and right children are stored at position 2i+1, and 2i+2 respectively. H is the height of the tree and this space is used implicitly for the recursion stack.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step guide to solve this problem in Python:

  1. First, we need to create a Node class for the Binary Tree. This class will have a value and two pointers for the left and right child.
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
  1. Next, we need to create a function that will convert the linked list into a binary tree. This function will take the linked list and the index of the current node as input.
def convertListToBinaryTree(lst, start):
    # Base case for recursion
    if start < len(lst):
        node = Node(lst[start])
        node.left = convertListToBinaryTree(lst, 2 * start + 1)
        node.right = convertListToBinaryTree(lst, 2 * start + 2)
        return node
    return None
  1. Now, we need a function to print the level order traversal of the binary tree. We can use a queue for this purpose.
def printLevelOrder(root):
    if root is None:
        return
    queue = []
    queue.append(root)
    while len(queue) > 0:
        print(queue[0].data, end=" ")
        node = queue.pop(0)
        if node.left is not None:
            queue.append(node.left)
        if node.right is not None:
            queue.append(node.right)
  1. Finally, we can use these functions to solve the problem.
# Linked list representation of the complete binary tree
lst = [1, 2, 3, 4, 5, 6, 7]

# Convert the linked list into a binary tree
root = convertListToBinaryTree(lst, 0)

# Print the level order traversal of the binary tree
printLevelOrder(root)

This will print the level order traversal of the binary tree.

This problem has been solved

Similar Questions

Given the root of a binary tree, return the preorder traversal of its nodes' values. Example 1:Input: root = [1,null,2,3]Output: [1,2,3]Example 2:Input: root = []Output: []Example 3:Input: root = [1]Output: [1] Constraints:The number of nodes in the tree is in the range [0, 100].-100 <= Node.val <= 100 Follow up: Recursive solution is trivial, could you do it iteratively?

VERTICAL ORDER TRAVERSALYou are given root of a binary tree, your task is to print the nodes in order obtained via vertical traversal of the tree.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.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).Input format :The first line contains the number of test cases.For each test case: You are given a pointer to the root of the binary tree.Output format :Print the nodes in order obtained via vertical traversal of the tree. Refer the same Input/Output provided.Sample test cases :Input 1 :11 2 3Output 1 :2 1 3 Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.

void printLevel(struct Node* root, int level) { // Function to print nodes at a specific level of a BST}void printLevelOrder(struct Node* root) { // Function to print the level order traversal of a BST}

Invert the Binary TreeGiven the root of a binary tree, invert the tree, and return its root. Print the Inorder traversal of the inverted tree.Input format:The input consists of one line, space-separated elements denoting the nodes of the Binary Tree.Output format:Print the inorder traversal of the inverted tree in a single line.

If we were to print the following tree using pre-order traversal, what would you expect the output to be? .-------(098)-------. .--(012)--. .--(402)-------.(010) (054) (045) .--(128)--. (092) (065)98, 12, 10, 54, 402, 45, 128 ,92, 6510, 12, 54, 98, 45, 402, 92, 128, 6510, 54, 12, 45, 92, 65, 128, 402, 9898, 12, 402, 10, 54, 45, 128, 92, 65

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.