algorithm - Counting the number of unival subtrees in a binary tree -


i came across interview question of finding number of unival subtrees in binary tree. how can done?

int countunivals(node* head, bool* unival) {     if (!node) {         *unival = true;         return 0;     }     bool unil,unir;     int sum = countunivals(head->l, &unil) + countunivals(head->r, &unir);     if (unil && unir &&         (!head->l || head->l->val == head->val) &&          (!head->r || head->r->val == head->val)) {         sum++;         *unival = true;     }     return sum; } 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -