Given a pair of tree traversal, return true if a unique binary tree can be constructed otherwise false. Each traversal is represented with integer: 1 -> Preorder , 2 -> Inorder , 3 -> Postorder.
Input : 1 2
Output : true
Explanation : Answer is True.
It is possible to construct a unique binary tree. This is because the preorder traversal provides the root of the tree, and the inorder traversal helps determine the left and right subtrees.
Input : 2 2
Output : false
Explanation : Two inorder traversals are insufficient to uniquely determine a binary tree.
Input : 1 3
The concept of "uniqueness" implies that there must be only one binary tree that matches the provided traversal sequences. Without this requirement, multiple trees could fit the same traversals, leading to ambiguity.
Let’s examine the scenarios where two types of traversals—preorder, postorder, and inorder—are given:
When provided with just preorder and postorder traversals, it is not possible to uniquely reconstruct a binary tree. This is because multiple trees can produce the same preorder and postorder sequences, leaving room for ambiguity.
Preorder traversal starts with the root node and explores the left subtree before the right subtree. This traversal identifies the root and helps in constructing the tree structure. In contrast, inorder traversal processes nodes by visiting the left subtree first, followed by the root, and then the right subtree. This approach clearly separates the left and right subtrees, enabling a unique reconstruction of the binary tree.
Postorder traversal visits nodes in the left subtree, then the right subtree, and finally the root node. The last element represents the root, while preceding elements identify subtrees. Inorder traversal provides a clear division between left and right subtrees. This combination allows for the unique construction of the binary tree.
Inorder traversal is crucial for constructing a unique binary tree. Preorder and postorder traversals alone do not provide explicit division between left and right subtrees, leading to multiple possible structures for nodes with a single child.
For a full binary tree, where every node has either zero or two children, the structure is fixed. Preorder and postorder traversals provide sufficient information to uniquely reconstruct the tree due to the absence of single-child nodes.
To determine if a unique binary tree can be constructed from two given traversals identify combinations that fail to provide sufficient information for unique reconstruction. Return false if the two traversals are the same, as they do not provide additional distinguishing information, or if the traversals are preorder and postorder, which cannot uniquely define a binary tree due to their inability to differentiate between certain tree structures. By checking these conditions, the solution ensures that only valid traversal pairs, which can uniquely define a binary tree, are considered.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Method to check if a unique binary tree can be constructed
bool uniqueBinaryTree(int a, int b) {
// Return false if both traversals are the same
// or if they are preorder and postorder
return !(a == b || (a == 1 && b == 3) || (a == 3 && b == 1));
}
};
int main() {
Solution solution;
// Example test cases
cout << solution.uniqueBinaryTree(1, 2) << endl;
cout << solution.uniqueBinaryTree(1, 3) << endl;
return 0;
}
class Solution {
// Method to check if a unique binary tree can be constructed
public boolean uniqueBinaryTree(int a, int b) {
// Return false if both traversals are the same
// or if they are preorder and postorder
return !(a == b || (a == 1 && b == 3) || (a == 3 && b == 1));
}
public static void main(String[] args) {
Solution solution = new Solution();
// Example test cases
System.out.println(solution.uniqueBinaryTree(1, 2));
System.out.println(solution.uniqueBinaryTree(1, 3));
}
}
class Solution:
# Method to check if a unique binary tree can be constructed
def unique_binary_tree(self, a, b):
# Return false if both traversals are the same
# or if they are preorder and postorder
return not (a == b or (a == 1 and b == 3) or (a == 3 and b == 1))
if __name__ == "__main__":
solution = Solution()
# Example test cases
print(solution.unique_binary_tree(1, 2))
print(solution.unique_binary_tree(1, 3))
class Solution {
// Method to check if a unique binary tree can be constructed
uniqueBinaryTree(a, b) {
// Return false if both traversals are the same
// or if they are preorder and postorder
return !(a === b || (a === 1 && b === 3) || (a === 3 && b === 1));
}
}
const solution = new Solution();
// Example test cases
console.log(solution.uniqueBinaryTree(1, 2));
console.log(solution.uniqueBinaryTree(1, 3));
Time Complexity: The time complexity of this solution is O(1) because it involves only a few constant-time comparisons and logical operations. There are no loops or recursive calls that depend on the input size.
Space Complexity: The space complexity of this solution is also O(1) since it uses a constant amount of extra space, regardless of the size of the input. The method only involves a few integer variables and does not allocate additional memory based on the input size.
Q: Why do we need Inorder for uniqueness?
A: Inorder preserves relative left and right positions, ensuring a consistent tree structure. Without Inorder, Preorder and Postorder alone are ambiguous.
Q: Can a tree be reconstructed from only one traversal?
A: No, unless additional constraints (like BST properties) are given. A single traversal does not provide enough information about left vs. right children.
Q: What if additional information (like parent-child relationships) was given?
A: If explicit parent-child mappings were provided, we could reconstruct without needing two traversals.
Q: Can this be extended to ternary or quaternary trees?
A: More than two traversals would be needed since binary tree rules do not apply.