Given a binary tree and data value of a node. The task is to find the sum of cousin nodes of given node. If given node has no cousins then return -1.
Note: It is given that all nodes have distinct values and the given node exists in the tree.
Input: 1 / \ 3 7 / \ / \ 6 5 4 13 / / \ 10 17 15 key = 13 Output: 11 Cousin nodes are 5 and 6 which gives sum 11. Input: 1 / \ 3 7 / \ / \ 6 5 4 13 / / \ 10 17 15 key = 7 Output: -1 No cousin nodes of node having value 7.
Approach: The approach is to do a level order traversal of the tree. While performing level order traversal, find the sum of child nodes of next level. Add a child node’s value to the sum and check if either of the children nodes is the target node or not. If yes, then do not add the value of either child to the sum. After traversing current level if the target node is present in next level, then end the level order traversal and sum found is the sum of cousin nodes.
Below is the implementation of the above approach:
“”” Python3 program to find sum of cousins
of given node in binary tree “””
# A Binary Tree Node
# Utility function to create a new tree node
# Constructor to create a newNode
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Function to find sum of cousins of
# a given node.
def findCousinSum( root, key):
if (root == None):
# Root node has no cousins so return -1.
if (root.data == key):
# To store sum of cousins.
currSum = 0
# To store size of current level.
size = 0
# To perform level order traversal.
q = 
# To represent that target node is
found = False
# If target node is present at
# current level, then return
# sum of cousins stored in currSum.
if (found == True):
# Find size of current level and
# traverse entire level.
size = len(q)
currSum = 0
root = q
# Check if either of the existing
# children of given node is target
# node or not. If yes then set
# found equal to true.
if ((root.left and root.left.data == key) or
(root.right and root.right.data == key)) :
found = True
# If target node is not children of current
# node, then its childeren can be cousin
# of target node, so add their value to sum.
currSum += root.left.data
if (root.right) :
currSum += root.right.data
size -= 1
# Driver Code
if __name__ == ‘__main__’:
/ \ / \
6 5 4 13
/ / \
10 17 15
root = newNode(1)
root.left = newNode(3)
root.right = newNode(7)
root.left.left = newNode(6)
root.left.right = newNode(5)
root.left.right.left = newNode(10)
root.right.left = newNode(4)
root.right.right = newNode(13)
root.right.left.left = newNode(17)
root.right.left.right = newNode(15)
# This code is contributed by
Time Complexity: O(N)
Auxiliary Space: O(N)
- Print cousins of a given node in Binary Tree | Single Traversal
- Check if two nodes are cousins in a Binary Tree | Set-2
- Check if two nodes are cousins in a Binary Tree
- Kth ancestor of a node in binary tree | Set 2
- K-th ancestor of a node in Binary Tree | Set 3
- K-th ancestor of a node in Binary Tree
- Search a node in Binary Tree
- Preorder predecessor of a Node in Binary Tree
- Kth node in Diagonal Traversal of Binary Tree
- Total sum except adjacent of a given node in a Binary Tree
- Get last node of the binary tree following given pattern starting from X
- Preorder Successor of a Node in Binary Tree
- Inorder Successor of a node in Binary Tree
- Find the node with maximum value in a Binary Search Tree
- Print the number of set bits in each node of a Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : SHUBHAMSINGH10