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 traversals (Inorder, Preorder, Postorder, level order traversal) and keep printing nodes that have mode left and right children as non-NULL.
Implementation:
C++
#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;
}
void findFullNode(Node *root)
{
if (root != NULL)
{
findFullNode(root->left);
if (root->left != NULL && root->right != NULL)
cout << root->data << " " ;
findFullNode(root->right);
}
}
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
public class FullNodes {
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);
}
}
class Node
{
int data;
Node left, right;
Node( int data)
{
left=right= null ;
this .data=data;
}
};
|
Python3
class newNode:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
def findFullNode(root) :
if (root ! = None ) :
findFullNode(root.left)
if (root.left ! = None and
root.right ! = None ) :
print (root.data, end = " " )
findFullNode(root.right)
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)
|
C#
using System;
public class FullNodes
{
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);
}
}
class Node
{
public int data;
public Node left, right;
public Node( int data)
{
left = right = null ;
this .data = data;
}
};
|
Javascript
<script>
class Node
{
constructor(data)
{
this .left= this .right= null ;
this .data=data;
}
}
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);
</script>
|
Time Complexity : O(n)
Space complexity: O(n) for Recursive Stack Space in case of Skewed Tree
This article is contributed by Rakesh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.