Open In App

Print all full nodes in a Binary Tree

Last Updated : 13 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Implementation:

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


Javascript




<script>
  
// JavaScript program to find the all full nodes in 
// a given binary tree 
  
/* A binary tree node */
class Node 
{
    constructor(data)
    {
        this.left=this.right=null;
        this.data=data;
    }
}
  
// Traverses given tree in Inorder fashion and 
    // prints all nodes that have both children as 
    // non-empty. 
function findFullNode(root)
{
    if (root != null
        
            findFullNode(root.left); 
            if (root.left != null && root.right != null
                document.write(root.data+" ");
            findFullNode(root.right); 
        
}
  
let 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); 
  
  
// This code is contributed by rag2127
  
</script>


Output

1 3 

Time Complexity : O(n)
Space complexity: O(n) for Recursive Stack Space in case of Skewed Tree

 



Similar Reads

Print all possible N-nodes Full Binary Trees
Given an integer N, the task is to print all possible Full Binary Trees with N nodes. The value at the nodes does not contribute to be a criteria for different Full Binary Tree, except for NULL,so take them as 0. A full binary tree is a binary tree in which every node has exactly 0 or 2 children. Examples: Input: N = 7Output: [[0, 0, 0, null, null,
15 min read
Check whether a binary tree is a full binary tree or not | Iterative Approach
Given a binary tree containing n nodes. The problem is to check whether the given binary tree is a full binary tree or not. A full binary tree is defined as a binary tree in which all nodes have either zero or two child nodes. Conversely, there is no node in a full binary tree, which has only one child node. Examples: Input : 1 / \ 2 3 / \ 4 5 Outp
8 min read
Check whether a binary tree is a full binary tree or not
A full binary tree is defined as a binary tree in which all nodes have either zero or two child nodes. Conversely, there is no node in a full binary tree, which has one child node. More information about full binary trees can be found here. For Example : Recommended PracticeFull binary treeTry It! To check whether a binary tree is a full binary tre
14 min read
Print all internal nodes of a Binary tree
Given a Binary tree, the task is to print all the internal nodes in a tree. An internal node is a node which carries at least one child or in other words, an internal node is not a leaf node. Here we intend to print all such internal nodes in level order. Consider the following Binary Tree: Input: Output: 15 10 20 The way to solve this involves a B
7 min read
Print all even nodes of Binary Search Tree
Given a binary search tree. The task is to print all even nodes of the binary search tree. Examples: Input : 5 / \ 3 7 / \ / \ 2 4 6 8 Output : 2 4 6 8 Input : 14 / \ 12 17 / \ / \ 8 13 16 19 Output : 8 12 14 16 Approach: Traverse the Binary Search tree and check if current node's value is even. If yes then print it otherwise skip that node. Below
8 min read
Print Sum and Product of all Non-Leaf nodes in Binary Tree
Given a Binary tree. The task is to find and print the product and sum of all internal nodes (non-leaf nodes) in the tree. In the above tree, only two nodes 1 and 2 are non-leaf nodes. Therefore, product of non-leaf nodes = 1 * 2 = 2. And sum of non-leaf nodes = 1 + 2 =3. Examples: Input : 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Output : Product = 36, Sum =
8 min read
Print all nodes between two given levels in Binary Tree
Given a binary tree, print all nodes between two given levels in a binary tree. Print the nodes level-wise, i.e., the nodes for any level should be printed from left to right. In the above tree, if the starting level is 2 and the ending level is 3 then the solution should print: 2 3 4 5 6 7 Note: Level number starts with 1. That is, the root node i
15 min read
Print all odd nodes of Binary Search Tree
Given a binary search tree. The task is to print all odd nodes of the binary search tree. Examples: Input : 5 / \ 3 7 / \ / \ 2 4 6 8 Output : 3 5 7 Input : 14 / \ 12 17 / \ / \ 8 13 16 19 Output : 13 17 19 Approach: Traverse the Binary Search tree using any of the tree traversals and check if the current node’s value is odd. If yes then print it o
8 min read
Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
Given a Binary Tree, the task is to print the leaf nodes from left to right. The nodes must be printed in the order they appear from left to right.Examples: Input : 1 / \ 2 3 / \ / \ 4 5 6 7 Output :4 5 6 7 Input : 4 / \ 5 9 / \ / \ 8 3 7 2 / / \ 12 6 1 Output :12 3 7 6 1 We have already discussed the Recursive approach. Here we will solve the prob
8 min read
Print all leaf nodes of a binary tree from right to left
Given a binary tree, the task is to print all the leaf nodes of the binary tree from right to left. Examples: Input : 1 / \ 2 3 / \ / \ 4 5 6 7 Output : 7 6 5 4 Input : 1 / \ 2 3 / \ \ 4 5 6 / / \ 7 8 9 Output : 9 8 7 4 Recursive Approach: Traverse the tree in Preorder fashion, by first processing the root, then right subtree and then left subtree
14 min read
Article Tags :
Practice Tags :