Given a binary tree, for each node check whether it is an internal node or not.
Examples:
Input:
Example-1
Output: Node A: True Node B: True Node C: False Node D: False Explanation: In this illustration, Node A possesses two child nodes (B and C) which categorizes it as a node. On the other hand, both Nodes B possess one child node D, and C is a leaf node since they lack any child nodes.
Input:
Example-2
Output: Node X: True Node Y: False Node Z: False Explanation: In this illustration, Node X possesses two child nodes (Y and Z) which categorizes it as a node. On the other hand, both Nodes Y and Z are leaf nodes since they lack any child nodes.
Approach: To solve the problem follow the below steps:
Traverse the tree starting from the root node.
For every encountered node, during traversal check if it has any child nodes or not.
If the node has at least one child classify it as an internal node; otherwise consider it a leaf node.
Check for at least one child if either left or right or both exist then the node is an internal node.
Below is the implementation of the above approach:
C++
// C++ implementation#include<iostream>#include<vector>classTreeNode{public:std::stringvalue;std::vector<TreeNode*>childNodes;TreeNode(std::stringval){value=val;}};boolcheckNode(TreeNode*currNode){returncurrNode->childNodes.size()>0;}intmain(){// Defining the tree node classTreeNode*root=newTreeNode("A");root->childNodes.push_back(newTreeNode("B"));root->childNodes.push_back(newTreeNode("C"));root->childNodes[0]->childNodes.push_back(newTreeNode("D"));// Checking internal nodesstd::cout<<checkNode(root)<<std::endl;std::cout<<checkNode(root->childNodes[0])<<std::endl;std::cout<<checkNode(root->childNodes[1])<<std::endl;std::cout<<checkNode(root->childNodes[0]->childNodes[0])<<std::endl;return0;}// This code is contributed by Tapesh(tapeshdua420)
Java
importjava.util.ArrayList;importjava.util.List;classTreeNode{Stringvalue;List<TreeNode>childNodes;// TreeNode class constructorpublicTreeNode(Stringval){value=val;childNodes=newArrayList<>();}}publicclassMain{// Function to check if the TreeNode has child nodespublicstaticbooleancheckNode(TreeNodecurrNode){returncurrNode.childNodes.size()>0;}publicstaticvoidmain(String[]args){// Defining the tree node class// Creating the root node 'A'TreeNoderoot=newTreeNode("A");// Adding child nodes 'B' and 'C' to the rootroot.childNodes.add(newTreeNode("B"));root.childNodes.add(newTreeNode("C"));// Adding a child node 'D' to node 'B'root.childNodes.get(0).childNodes.add(newTreeNode("D"));// Checking internal nodes// Checking if the root node has child nodesSystem.out.println(checkNode(root));// Checking if node 'B' has child nodesSystem.out.println(checkNode(root.childNodes.get(0)));// Checking if node 'C' has child nodesSystem.out.println(checkNode(root.childNodes.get(1)));// Checking if node 'D' has child nodesSystem.out.println(checkNode(root.childNodes.get(0).childNodes.get(0)));}}
Python
# Defining the tree node classclasstree:def__init__(self,val):self.value=valself.childnode=[]# Here we will check if the node is# internal or notdefcheckNode(currNode):returnlen(currNode.childnode)>0# Now we are adding nodes in the treeroot=tree("A")root.childnode.append(tree("B"))root.childnode.append(tree("C"))root.childnode[0].childnode.append(tree("D"))# Checking internal nodesprint(checkNode(root))print(checkNode(root.childnode[0]))print(checkNode(root.childnode[1]))print(checkNode(root.childnode[0].childnode[0]))
C#
usingSystem;usingSystem.Collections.Generic;// Defining the tree node classclassTreeNode{publicstringValue{get;set;}publicList<TreeNode>ChildNodes{get;}publicTreeNode(stringval){Value=val;ChildNodes=newList<TreeNode>();}}classMainClass{// Here we will check if the node is internal or notstaticboolCheckNode(TreeNodecurrNode){returncurrNode.ChildNodes.Count>0;}publicstaticvoidMain(string[]args){// Now we are adding nodes in the treeTreeNoderoot=newTreeNode("A");root.ChildNodes.Add(newTreeNode("B"));root.ChildNodes.Add(newTreeNode("C"));root.ChildNodes[0].ChildNodes.Add(newTreeNode("D"));// Checking internal nodesConsole.WriteLine(CheckNode(root));// Output: TrueConsole.WriteLine(CheckNode(root.ChildNodes[0]));// Output: TrueConsole.WriteLine(CheckNode(root.ChildNodes[1]));// Output: FalseConsole.WriteLine(CheckNode(root.ChildNodes[0].ChildNodes[0]));// Output: False}}
JavaScript
// Define the tree node classclassTreeNode{constructor(val){this.value=val;this.childNodes=[];}}// Function to check if a node is internal or notfunctioncheckNode(currNode){returncurrNode.childNodes.length>0;}// Create the root node and build the tree structureconstroot=newTreeNode("A");root.childNodes.push(newTreeNode("B"));root.childNodes.push(newTreeNode("C"));root.childNodes[0].childNodes.push(newTreeNode("D"));// Checking if the nodes are internalconsole.log(checkNode(root));console.log(checkNode(root.childNodes[0]));console.log(checkNode(root.childNodes[1]));console.log(checkNode(root.childNodes[0].childNodes[0]));
Output
True
True
False
False
Time Complexity: O(N), where N is the number of nodes in the tree. Auxillary Space: O(H), where H is the height of the tree (due to the recursion stack).