Given a binary tree, the task is to print all the nodes having exactly one child. Print “-1” if no such node exists.

**Examples:**

Input:2 / \ 3 5 / / \ 7 8 6Output:3Explanation:There is only one node having single child that is 3.Input:9 / \ 7 8 / \ 4 3Output:-1Explanation:There is no node having exactly one child in the binary tree.

**Approach:** The idea is to traverse the tree in the inorder traversal and at each step of the traversal check that if the node is having exactly one child. Then append that node into a result array to keep track of such nodes. After the traversal, simply print each element of this result array.

Below is the implementation of the above approach:

## Python

`# Python implementation to print ` `# the nodes having a single child ` ` ` `# Class of the Binary Tree node ` `class` `node: ` ` ` ` ` `# Constructor to construct ` ` ` `# the node of the Binary tree ` ` ` `def` `__init__(` `self` `, val): ` ` ` `self` `.val ` `=` `val ` ` ` `self` `.left ` `=` `None` ` ` `self` `.right ` `=` `None` ` ` `# List to keep track of ` `# nodes having single child ` `single_child_nodes ` `=` `[] ` ` ` `# Function to find the nodes ` `# having single child ` `def` `printNodesOneChild(root): ` ` ` ` ` `# Base Case ` ` ` `if` `not` `root: ` ` ` `return` ` ` ` ` `# Condition to check if the node ` ` ` `# is having only one child ` ` ` `if` `not` `root.left ` `and` `root.right: ` ` ` `single_child_nodes.append(root) ` ` ` `elif` `root.left ` `and` `not` `root.right: ` ` ` `single_child_nodes.append(root) ` ` ` ` ` `# Traversing the left child ` ` ` `printNodesOneChild(root.left) ` ` ` ` ` `# Traversing the right child ` ` ` `printNodesOneChild(root.right) ` ` ` `return` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# Condtruction of Binary Tree ` ` ` `root ` `=` `node(` `2` `) ` ` ` `root.left ` `=` `node(` `3` `) ` ` ` `root.right ` `=` `node(` `5` `) ` ` ` `root.left.left ` `=` `node(` `7` `) ` ` ` `root.right.left ` `=` `node(` `8` `) ` ` ` `root.right.right ` `=` `node(` `6` `) ` ` ` ` ` `# Function Call ` ` ` `printNodesOneChild(root) ` ` ` ` ` `# Condition to check if there is ` ` ` `# no such node having single child ` ` ` `if` `not` `len` `(single_child_nodes): ` ` ` `print` `(` `-` `1` `) ` ` ` `else` `: ` ` ` `for` `i ` `in` `single_child_nodes: ` ` ` `print` `(i.val, end ` `=` `" "` `) ` ` ` `print` `() ` |

*chevron_right*

*filter_none*

**Output:**

3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of all the child nodes with even grandparents in a Binary Tree
- Sum of all the child nodes with even parent values in a Binary Tree
- Count of nodes in a Binary Tree whose child is its prime factors
- Print nodes in top view of Binary Tree | Set 2
- Print nodes in the Top View of Binary Tree | Set 3
- Print all even nodes of Binary Search Tree
- Print all internal nodes of a Binary tree
- Print path between any two nodes in a Binary Tree | Set 2
- Print the nodes of Binary Tree having a grandchild
- Print path between any two nodes in a Binary Tree
- Print Levels of all nodes in a Binary Tree
- Print all nodes between two given levels in Binary Tree
- Print Nodes in Top View of Binary Tree
- Print all full nodes in a Binary Tree
- Print all nodes in a binary tree having K leaves
- Print leftmost and rightmost nodes of a Binary Tree
- Print the nodes of binary tree as they become the leaf node
- Print all leaf nodes of a Binary Tree from left to right
- Print nodes between two given level numbers of a binary tree
- Print all leaf nodes of a binary tree from right to left

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.