Create Preorder Using Postorder and Leaf Nodes Array10 Sept 2024 | 4 min read In input, there are two arrays that are given to us. One array is an integer array that represents postorder traversal of a binary tree, and the other array is a Boolean array that provides information about the leaf nodes. For every element in the postorder array, there is a corresponding Boolean value in the leaf nodes array. If the value in the Boolean array is true, then the given elements will comprise a leaf node in the binary tree; otherwise, it will not. Our task is to display the preorder traversal of the binary tree whose postorder traversal is given in the input. Note that there might be a possibility that multiple binary trees exist of the same postorder traversal. One has to consider any one of the binary trees and according to that preorder traversal has to be displayed on the console. Example 1: Input postorder = { 140, 120, 150, 160, 130, 110, 170 } isLeaf = { true, false, true, true, false, false, false } Output: { 170, 110, 120, 140, 130, 150, 160 } ApproachThe concept is to first identify the root node of the binary tree with the help of the last key in the postorder sequence. Then with the help of the given Boolean array, we check whether the root node is a leaf node or an internal node. If the root node is an internal node, we recursively create its left and right subtrees. The following is the implementation of the mentioned approach. FileName: CreatePreorder.java Output: For a tree whose post order traversal is: 140 120 150 160 130 110 170 The preorder traversal is: 170 110 120 140 130 150 160 Complexity Analysis: The time complexity, as well as the space complexity of the program, is O(n), where n is the number of elements present in the preorder array. |
print() Vs. println() in Java
Many techniques are available in Java, a popular object-oriented programming language, for delivering data to the console. Two methods that are frequently used for this are print() and println(). Even while they might look the same, there are a few subtle variations that could change how...
3 min read
Default Parameter in Java
In Java, default parameters are a powerful feature that allows developers to define default values for method parameters. This can be useful when a method has a large number of parameters, and some of them are not always required. Default parameters were introduced in Java 8 and...
4 min read
Definite Assignment in Java
Every local variable and the final blank field will have an assigned value when any value is accessed. Access to the value will consist of the variable's name or an area that occurs in an expression, except in the left-hand operand of the assignment operator, "=". To...
15 min read
Package getName() method in Java with Examples
The java.lang.Package class contains getName() function. The package name can be obtained by using the package class. The package name is returned as a String by this method. Syntax: public String getName() Parameter: There are no parameters accepted by this method. Return Value: The package name is...
2 min read
Input from console in Java
Reading data from keyboard There are many ways to read data from the keyboard. For example: InputStreamReader Console Scanner DataInputStream etc. InputStreamReader class InputStreamReader class can be used to read data from keyboard.It performs two tasks: connects to input stream of keyboard converts the byte-oriented stream into character-oriented stream BufferedReader class BufferedReader class can be used...
1 min read
Java Program to Find the Value of a Number Raised to Its Reverse
Calculating the value of a number raised to its reverse offers a captivating blend of arithmetic and numerical exploration. This intriguing concept invites curiosity about the interplay between numbers and their reversals, highlighting the beauty of mathematical patterns and relationships. Problem Statement We have given a number P...
4 min read
Is the main() method compulsory in Java
? Java main() method that serves as both the program's entry point and the Java Virtual Machine's (JVM) launchpad, is an essential part of Java programs. However, there are situations where a Java program may not contain a main() method. Method Signature public static void main(String[] args) { ...
4 min read
How to Make Object Serializable in Java
? The process of serialising an object in Java so that it can be saved to a file, transmitted over a network, or kept in a database is known as serialisation. The original object can then be recreated using this byte stream, with all of its...
5 min read
Java Program to Form Coils in a Matrix
Creating particular patterns in a 2D grid that resemble spirals or concentric loops is known as "forming coils in a matrix." Finding an organized traversal of the matrix elements where the values are grouped in a sequential and structured fashion is usually necessary for this operation....
7 min read
Java vs PHP
Difference Between Java and PHP Java and PHP are both the two most popular programming languages. There are so many differences and similarities between both of them. Let's understand both of them one by one, and after that, we understand the similarities and differences between them. Java Java is...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India