Invert Binary Tree – Iterative and Recursive Solution
Given a binary tree, write an efficient algorithm to invert it.
Ace your Coding Interview
Get hired by top tech companies with our comprehensive interview preparation.
Get StartedGiven a binary tree, write an efficient algorithm to invert it.
Given a maze in the form of a rectangular matrix, filled with either O
, X
, or M
, where O
represents an open cell, X
represents a blocked cell, and M
represents landmines in the maze, find the shortest distance of every open cell in the maze from its nearest mine.
Given a binary tree, print corner nodes of every level in it.
Given a binary tree, print all nodes for each diagonal having negative slope (\)
. Assume that the left and right child of a node makes a 45–degree angle with the parent.
Given a binary tree, perform vertical traversal on it. In vertical traversal, nodes of a binary tree are printed in vertical order. Assume that the left and right child makes a 45–degree angle with the parent.
Given a binary tree, check if it is a complete binary tree or not. A complete binary tree is a binary tree in which every level, except possibly the last, is filled, and all nodes are as far left as possible.
Given a binary tree and a node in it, write an efficient algorithm to find its next node at the same level as the node.
Given a binary tree, write an efficient algorithm to print its left view. For example, the left view of the following binary tree is 1, 2, 4, 7
.
Given a binary tree, write an efficient algorithm to print all nodes in a specific order. We need to print nodes of every level in alternating left and right.
Given a binary tree, print its nodes level by level in reverse order, i.e., print all nodes present at the last level first, followed by nodes of the second last level, and so on… Print nodes at any level from left to right.
Given a binary tree, print its nodes level by level in spiral order, i.e., all nodes present at level 1 should be printed first from left to right, followed by nodes of level 2 right to left, followed by nodes of level 3 from left to right and so on…
Given a binary tree, print its nodes level by level, i.e., print all nodes of level 1 first, followed by nodes of level 2 and so on… Print nodes for any level from left to right.