Problem statementsGiven a binary tree, find its height.Input FormatA single line of input contains string s which contains the node of the tree in level order fashion.'N' in the string represents NULL node.A string 5 1 1 2 N N 2 reprsents the tree 5 / \ 1 1 / \ 2 2Similarly a string 1 2 N 3 N represents the tree 1 / 2 / 3Constraints1 <= Number of nodes <= 10^51 <= Data of a node <= 10^5Output FormatPrint the height of the tree.Sample Input2 N 1 3 NSample Output3
Question
Problem statementsGiven a binary tree, find its height.Input FormatA single line of input contains string s which contains the node of the tree in level order fashion.'N' in the string represents NULL node.A string 5 1 1 2 N N 2 reprsents the tree 5 / \ 1 1 / \ 2 2Similarly a string 1 2 N 3 N represents the tree 1 / 2 / 3Constraints1 <= Number of nodes <= 10^51 <= Data of a node <= 10^5Output FormatPrint the height of the tree.Sample Input2 N 1 3 NSample Output3
Solution
The problem is asking to find the height of a binary tree. The binary tree is given in a level order fashion where 'N' represents a NULL node.
Here are the steps to solve this problem:
-
First, we need to construct the binary tree from the given level order traversal. We can do this by using a queue data structure. Initialize the queue and push the root into the queue.
-
Then, we start a while loop until the queue is empty. In each iteration, we remove the node at the front of the queue. If the removed node has a left child, we push it into the queue. Similarly, if the removed node has a right child, we push it into the queue.
-
After constructing the binary tree, we can find its height. The height of a binary tree is the maximum depth of its nodes. We can find the height by using a recursive function. The base case for the recursion is when the node is NULL, in which case we return 0. Otherwise, we return the maximum of the height of the left subtree and the height of the right subtree, plus 1.
-
Finally, we print the height of the binary tree.
Here is a Python solution for the problem:
from collections import deque
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def construct_tree(levelorder):
if levelorder[0] == 'N':
return None
root = Node(int(levelorder[0]))
queue = deque([root])
i = 1
while queue and i < len(levelorder):
node = queue.popleft()
if levelorder[i] != 'N':
node.left = Node(int(levelorder[i]))
queue.append(node.left)
i += 1
if i < len(levelorder) and levelorder[i] != 'N':
node.right = Node(int(levelorder[i]))
queue.append(node.right)
i += 1
return root
def height(root):
if root is None:
return 0
return max(height(root.left), height(root.right)) + 1
levelorder = input().split()
root = construct_tree(levelorder)
print(height(root))
This program reads the level order traversal from the input, constructs the binary tree, finds its height, and prints the height.
Similar Questions
Level Order of TreeGiven an array of unique elements, construct a Binary Search Tree and print the Level Order of the tree.Input FormatFirst line of each test case contains N - number of nodes in the BST. The next line contains N unique integers - value of the nodes.ConstraintsPrint the Level Order of the Binary Search Tree, separate each level by newline.Output Format1 <= N <= 10000 <= ar[i] <= 10000Sample input6 10 5 15 2 7 12Sample output10 5 15 2 7 12
Problem statementYou are given a binary tree having nodes in string format.The boundary nodes of a binary tree include the nodes from the left and right boundaries and the leaf nodes, each node considered once.Figure out the boundary nodes of this binary tree in an Anti-Clockwise direction starting from the root node.Input format :List of numbers in string formatOutput format :Boundary nodes of a given binary treeSample test cases :Input 1 :3 4 5 6 7 8 9 10Output 1 :3 4 6 10 7 8 9 5
Tree: Height of a Binary Tree
Suppose a binary tree is constructed with n nodes, such that each node has exactly either zero or two children. The maximum height of the tree will be
Question3Max. score: 100.00Level Order of TreeGiven an array of unique elements, construct a Binary Search Tree and print the Level Order of the tree.Input FormatFirst line of each test case contains N - number of nodes in the BST. The next line contains N unique integers - value of the nodes.ConstraintsPrint the Level Order of the Binary Search Tree, separate each level by newline.Output Format1 <= N <= 10000 <= ar[i] <= 10000Sample input6 10 5 15 2 7 12Sample output10 5 15 2 7 12
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.