06(May) Print all nodes that don't have sibling
06. Print All Nodes That Don't Have Sibling
The problem can be found at the following link: Question Link
Problem Description
Given a Binary Tree of (n) nodes, find all the nodes that don't have any siblings. You need to return a list of integers containing all the nodes that don't have a sibling in sorted order (increasing).
Two nodes are said to be siblings if they are present at the same level, and their parents are the same.
Example 1:
Input:
37
/
20
/
113Output:
20 113Explanation: Nodes 20 and 113 don't have any siblings.
Example 2:
Input:
Output:
Explanation: Every node has a sibling.
Approach
Traversal:
Implement a depth-first traversal of the binary tree.
During the traversal, keep track of nodes that don't have siblings.
Sorting and Return:
Sort the list of nodes that don't have siblings in increasing order.
If all nodes have siblings, return a list containing only -1.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(nlogn), where (n) is the number of nodes in the binary tree. We traverse the entire tree once, and sorting the result takes (O(nlogn)) time.
Expected Auxiliary Space Complexity: O(height of the tree), as the space required is proportional to the height of the tree due to recursive function calls.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated