Given a binary tree, print all nodes will are full nodes. Full Nodes are nodes which has both left and right children as non-empty.
Examples:
Input : 10
/ \
8 2
/ \ /
3 5 7
Output : 10 8
Input : 1
/ \
2 3
/ \
4 6
Output : 1 3
This is a simple problem. We do any of the traÂverÂsals (Inorder, PreÂorder, PosÂtorder, level order traversal) and keep printing nodes that have mode left and right children as non-NULL.
C++
// A C++ program to find the all full nodes in // a given binary tree #include <bits/stdc++.h> using namespace std; struct Node { int data; struct Node *left, *right; }; Node *newNode(int data) { Node *temp = new Node; temp->data = data; temp->left = temp->right = NULL; return temp; } // Traverses given tree in Inorder fashion and // prints all nodes that have both children as // non-empty. void findFullNode(Node *root) { if (root != NULL) { findFullNode(root->left); if (root->left != NULL && root->right != NULL) cout << root->data << " "; findFullNode(root->right); } } // Driver program to test above function int main() { Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->right->left = newNode(5); root->right->right = newNode(6); root->right->left->right = newNode(7); root->right->right->right = newNode(8); root->right->left->right->left = newNode(9); findFullNode(root); return 0; } |
Java
// Java program to find the all full nodes in // a given binary tree public class FullNodes { // Traverses given tree in Inorder fashion and // prints all nodes that have both children as // non-empty. public static void findFullNode(Node root) { if (root != null) { findFullNode(root.left); if (root.left != null && root.right != null) System.out.print(root.data+" "); findFullNode(root.right); } } public static void main(String args[]) { Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.right.left = new Node(5); root.right.right = new Node(6); root.right.left.right = new Node(7); root.right.right.right = new Node(8); root.right.left.right.left = new Node(9); findFullNode(root); } } /* A binary tree node */class Node { int data; Node left, right; Node(int data) { left=right=null; this.data=data; } }; //This code is contributed by Gaurav Tiwari |
Python3
# Python3 program to find the all # full nodes in a given binary tree # Binary Tree Node """ utility that allocates a newNode with the given key """class newNode: # Construct to create a newNode def __init__(self, key): self.data = key self.left = None self.right = None # Traverses given tree in Inorder # fashion and prints all nodes that # have both children as non-empty. def findFullNode(root) : if (root != None) : findFullNode(root.left) if (root.left != None and root.right != None) : print(root.data, end = " ") findFullNode(root.right) # Driver Code if __name__ == '__main__': root = newNode(1) root.left = newNode(2) root.right = newNode(3) root.left.left = newNode(4) root.right.left = newNode(5) root.right.right = newNode(6) root.right.left.right = newNode(7) root.right.right.right = newNode(8) root.right.left.right.left = newNode(9) findFullNode(root) # This code is contributed by # Shubham Singh(SHUBHAMSINGH10) |
C#
// C# program to find the all full nodes in // a given binary tree using System; public class FullNodes { // Traverses given tree in Inorder fashion and // prints all nodes that have both children as // non-empty. static void findFullNode(Node root) { if (root != null) { findFullNode(root.left); if (root.left != null && root.right != null) Console.Write(root.data + " "); findFullNode(root.right); } } public static void Main(String []args) { Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.right.left = new Node(5); root.right.right = new Node(6); root.right.left.right = new Node(7); root.right.right.right = new Node(8); root.right.left.right.left = new Node(9); findFullNode(root); } } /* A binary tree node */class Node { public int data; public Node left, right; public Node(int data) { left = right = null; this.data = data; } }; // This code is contributed by 29AjayKumar |
Output:
1 3
Time Complexity : O(n)
This article is contributed by Rakesh Kumar. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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:
- Count full nodes in a Binary tree (Iterative and Recursive)
- Print path between any two nodes in a Binary Tree | Set 2
- Print Nodes in Top View of Binary Tree
- Print the nodes having exactly one child in a Binary tree
- Print the nodes of Binary Tree having a grandchild
- Print nodes in top view of Binary Tree | Set 2
- Print path between any two nodes in a Binary Tree
- Print all nodes in a binary tree having K leaves
- Print Levels of all nodes in a Binary Tree
- Print all even nodes of Binary Search Tree
- Print nodes in the Top View of Binary Tree | Set 3
- Print all internal nodes of a Binary tree
- Print all nodes between two given levels in Binary Tree
- Print the nodes of binary tree as they become the leaf node
- Print all leaf nodes of a binary tree from right to left
- Print nodes between two given level numbers of a binary tree
- Print all leaf nodes of a Binary Tree from left to right
- Print leftmost and rightmost nodes of a Binary Tree
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Print path from root to all nodes in a Complete Binary Tree

