OFFSET
1,2
COMMENTS
An ordered tree is balanced if all leaves are the same distance from the root.
The binary encoding of an ordered tree (see A014486) is obtained by replacing the internal left and right brackets with 0's and 1's, thus forming a binary number.
EXAMPLE
The terms together with their corresponding trees begin:
0: o
2: (o)
10: (oo)
12: ((o))
42: (ooo)
52: ((oo))
56: (((o)))
170: (oooo)
204: ((o)(o))
212: ((ooo))
232: (((oo)))
240: ((((o))))
682: (ooooo)
820: ((o)(oo))
844: ((oo)(o))
852: ((oooo))
920: (((o)(o)))
936: (((ooo)))
976: ((((oo))))
992: (((((o)))))
MATHEMATICA
binbalQ[n_]:=n==0||Count[IntegerDigits[n, 2], 0]==Count[IntegerDigits[n, 2], 1]&&And@@Table[Count[Take[IntegerDigits[n, 2], k], 0]<=Count[Take[IntegerDigits[n, 2], k], 1], {k, IntegerLength[n, 2]}];
bint[n_]:=If[n==0, {}, ToExpression[StringReplace[StringReplace[ToString[IntegerDigits[n, 2]/.{1->"{", 0->"}"}], ", "->""], "} {"->"}, {"]]]
Select[Range[0, 1000], binbalQ[#]&&SameQ@@Length/@Position[bint[#], {}]&]
CROSSREFS
KEYWORD
nonn
AUTHOR
Gus Wiseman, Nov 21 2022
STATUS
approved