
• The left subtree of a node contains only nodes with data less than the node’s data.
• The right subtree of a node contains only nodes with data greater than the node’s data.
• Both the left and right subtrees must also be binary search trees.
The first line contains an Integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of the input contains an integer ‘N’ representing the size of the array.
The second line of the input contains ‘N’ space-separated integers representing elements of the array ‘ARR’.
For each test case, print a string ‘S’ denoting if each non-leaf node of the tree has only one child or not. If each non-leaf has only one child then ‘S’ will be “true”, otherwise “false”.
Output for each test case will be printed in a separate line.
You are not required to print the output, it has already been taken care of. Just implement the function.
1 <= T <= 20
1 <= N <= 10^4
1 <= ARR[i] <= 10^5
Time Limit: 1 sec
In preorder traversal, we first visit the root node, then the left sub-tree, and then the right subtree. So all the descendants of a Node appear on the right in the pre-order traversal.
If a particular node has only one child, then all the descendants are either on the right subtree or left subtree. Hence the descendants are either all greater than the node or all less than the Node.
In this approach, we will take the last two elements of the array. Set the lesser one as ‘MIN’ and the greater one as ‘MAX’. Now each element should be lesser than ‘MIN’ and greater than ‘MAX’. We will keep updating ‘MAX’ and ‘MIN’ accordingly.
Guess Price
Unique BSTs
Unique BSTs
Unique BSTs
Kth Largest Element in BST
Two Sum IV - Input is a BST
Icarus and BSTCOUNT