Replace with depth
You are given a generic tree. You have to replace each node with its depth value. You just have to update the data of each node, there is no need to return or print anything.
Input format :
Output format:
Constraints:
Sample Input 1:
Sample Output 1:
2 2
/************************************************************
Following is the structure for the TreeNode class
template <typename T>
class TreeNode {
public:
T data;
vector<TreeNode<T>*> children;
TreeNode(T data) {
this->data = data;
}
~TreeNode() {
for (int i = 0; i < children.size(); i++) {
delete children[i];
}
}
};
************************************************************/
void helper(TreeNode<int> *root , int level){
root->data = level;
for(int i = 0 ; i< root->children.size() ; i++){
helper(root->children[i] , level+1);
}
}
void replaceWithDepthValue(TreeNode<int>* root) {
// Write your code here
int level = 0;
helper(root, level);
}
Comments
Post a Comment