Sum of Nodes of tree
Given a generic tree, find and return the sum of all nodes present in the given tree.
Input format :
Output Format :
Constraints:
Sample Input 1:
Sample Output 1:
190
int sumOfNodes(TreeNode<int>* root) {
// Write your code here
int ans=root->data;
for(int i=0;i<root->children.size();i++){
ans+=sumOfNodes(root->children[i]);
}
return ans;
}
Comments
Post a Comment