#include <iostream>
#include <iomanip>
struct Binode
{
char data ;
Binode* r ;
Binode* l ;
};
struct Indent {
static unsigned level ;
Indent() { std::cout << std::setw(level) << "" << "{\n"; level += 4 ; }
~Indent() { level -=4 ; std::cout << std::setw(level) << "" << "}\n" ; }
};
std::ostream& operator<<(std::ostream& os, const Indent & indent)
{
return os << std::setw(Indent::level) << "" ;
}
unsigned Indent::level = 0 ;
unsigned depth(Binode* p)
{
Indent indent ;
std::cout << indent ;
if ( p == nullptr )
{
std::cout << "NULL\n" ;
return 0 ;
}
std::cout << p->data << '\n' ;
std::cout << indent << p->data << ": right node\n" ;
unsigned rdepth = depth(p->r) ;
std::cout << indent << "rdepth: " << rdepth << '\n' ;
std::cout << indent << p->data << ": left node\n" ;
unsigned ldepth = depth(p->l) ;
std::cout << indent << "ldepth: " << ldepth << '\n' ;
std::cout << indent << "Returning: " << (rdepth < ldepth ? 1+ldepth : 1+rdepth) << '\n' ;
return rdepth < ldepth ? 1+ldepth : 1+rdepth ;
}
int main()
{
Binode a { 'a', nullptr, nullptr };
Binode b { 'b', &a, nullptr } ;
Binode c { 'c', &b, nullptr } ;
std::cout << depth(&c) << '\n' ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8aW9tYW5pcD4KIApzdHJ1Y3QgQmlub2RlCnsKICAgIGNoYXIgZGF0YSA7CiAgICBCaW5vZGUqIHIgOwogICAgQmlub2RlKiBsIDsKfTsKCnN0cnVjdCBJbmRlbnQgewogIHN0YXRpYyB1bnNpZ25lZCBsZXZlbCA7CiAgCiAgSW5kZW50KCkgeyBzdGQ6OmNvdXQgPDwgc3RkOjpzZXR3KGxldmVsKSA8PCAiIiA8PCAie1xuIjsgbGV2ZWwgKz0gNCA7IH0KICB+SW5kZW50KCkgeyBsZXZlbCAtPTQgOyAgc3RkOjpjb3V0IDw8IHN0ZDo6c2V0dyhsZXZlbCkgPDwgIiIgPDwgIn1cbiIgOyB9Cn07CgpzdGQ6Om9zdHJlYW0mIG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