Max node of tree

 #include <climits>

TreeNode<int>* maxDataNode(TreeNode<int>* root) {

    // Write your code here

    TreeNode<int>*max=new TreeNode<int>(root->data);

    

    if (max->data<root->data){

        max->data=root->data;

        max=root;

    }

    for(int i=0;i<root->children.size();i++){

        TreeNode<int>* a=maxDataNode(root->children[i]);

        if (a->data>max->data){

            max=a;

        }

        

    }

    return max;

}

Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings