Prepin
Log in

Print the cousins of a specified node in a binary tree.

Approach / Explanation

The query Print cousins of a given node in a binary tree can be interpreted in the following way: In a binary tree, cousins of a node are those nodes that are on the same level as the specified node but do not have the same parent. Essentially, the task is to return a list of these cousin nodes. To address this query, several important steps should be taken: Initially, we need to determine the level of the specified node. Next, we will traverse the tree to find the nodes that exist at the same level as the specified node. During this traversal, we must ensure that we exclude the siblings of the specified node from the list of cousins.

Suggested Answer

Examine the following Python code: def getLevel ( root , node , level ) : if root is None : return 0 if root == node : return level ret = getLevel ( root . left , node , level + 1 ) if ret != 0 : return ret return getLevel ( root . right , node , level + 1 ) def printCousins ( root , node ) : level = getLevel ( root , node , 1 ) printCousinsUtil ( root , node , level ) def printCousinsUtil ( root , node , level ) : if level < 2 or root is None : return elif level == 2 : if root . left == node or root . right == node : return if root . left : print ( root . left . data , end = " " ) if root . right : print ( root . right . data , end = " " ) else : printCousinsUtil ( root . left , node , level - 1 ) printCousinsUtil ( root . right , node , level - 1 ) This code first determines the level of the specified node using the getLevel function. Subsequently, printCousins invokes the printCousinsUtil function, which traverses the tree at the same level. The printCousinsUtil function is designed to exclude the siblings of the specified node from being printed. The output generated by the printCousins function will consist of the values of the cousins of the specified node within the tree.

Alternative Answer

In Java, the code might appear as follows: void printCousins ( Node root , Node nodeToFind ) { int nodeLevel = level ( root , nodeToFind , 1 ) ; printGivenLevel ( root , nodeToFind , nodeLevel ) ; } int level ( Node root , Node nodeToFind , int level ) { if ( root == null ) return 0 ; if ( root == nodeToFind ) return level ; int downlevel = level ( root . left , nodeToFind , level + 1 ) ; if ( downlevel != 0 ) return downlevel ; return level ( root . right , nodeToFind , level + 1 ) ; } void printGivenLevel ( Node root , Node nodeToFind , int level ) { if ( root == null || level < 2 ) return ; if ( level == 2 ) { if ( root . left == nodeToFind || root . right == nodeToFind ) return ; if ( root . left != null ) System . out . print ( root . left . data + " " ) ; else if ( root . right != null ) System . out . print ( root . right . data + " " ) ; } else if ( level > 2 ) { printGivenLevel ( root . left , nodeToFind , level - 1 ) ; printGivenLevel ( root . right , nodeToFind , level - 1 ) ; } } In this implementation, similar to the Python version, we first determine the level of the target node and then explore that specific level, ensuring that we do not print the siblings of the specified node.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.