Knowee
Questions
Features
Study Tools

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.

Question

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.

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

Solution

Here is a step-by-step solution to invert a binary tree and print its inorder traversal:

  1. Define a binary tree node structure. In Python, it would look something like this:
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
  1. Write a function to invert the binary tree. This function will take the root of the binary tree as input and return the root of the inverted tree. The inversion process involves swapping the left and right children of each node in the tree.
def invertTree(root):
    if root:
        root.left, root.right = invertTree(root.right), invertTree(root.left)
        return root
  1. Write a function to perform an inorder traversal of the binary tree. This function will take the root of the tree as input and print the values of the nodes in inorder sequence.
def inorderTraversal(root):
    if root:
        inorderTraversal(root.left)
        print(root.val, end=' ')
        inorderTraversal(root.right)
  1. Now, you can use these functions to invert a binary tree and print its inorder traversal. First, create the binary tree from the given input. Then, call the invertTree function to invert the tree. Finally, call the inorderTraversal function to print the inorder traversal of the inverted tree.
# create the binary tree from the given input
root = TreeNode(4)
root.left = TreeNode(2)
root.right = TreeNode(7)
root.left.left = TreeNode(1)
root.left.right = TreeNode(3)
root.right.left = TreeNode(6)
root.right.right = TreeNode(9)

# invert the binary tree
root = invertTree(root)

# print the inorder traversal of the inverted tree
inorderTraversal(root)

This will print the inorder traversal of the inverted binary tree.

This problem has been solved

Similar Questions

Given the root of a binary tree, invert the tree, and return its root. Example 1:Input: root = [4,2,7,1,3,6,9]Output: [4,7,2,9,6,3,1]

Given the root of a binary tree, return the leftmost value in the last row of the tree. Example 1:Input: root = [2,1,3]Output: 1

Complete the function in the editor below. It received parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values.

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.

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?

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.