Contains x
Send Feedback
Given a generic tree and an integer x, check if x is present in the given tree or not. Return true if x is present, return false otherwise.
Input format :
Output format :
Constraints:
Sample Input 1 :
Sample Output 1 :
Sample Input 2 :
Sample Output 2:
false
bool isPresent(TreeNode<int>* root, int x) {
// Write your code here
if (root==NULL){
return false;
}
if(root->data==x){
return true;
}
for(int i=0;i<root->children.size();i++){
bool ans=isPresent(root->children[i],x);
if (ans==true){
return true;
}
}
return false;
}
Comments
Post a Comment