1. var countNodes = function(root) {
    2. if (root == null) return 0;
    3. let l = root, r = root;
    4. let lh = 0, rh = 0;
    5. while (l != null) {
    6. l = l.left;
    7. lh++;
    8. }
    9. while (r != null) {
    10. r = r.right;
    11. rh++;
    12. }
    13. if (lh === rh) {
    14. return Math.pow(2, lh) - 1;
    15. }
    16. return 1 + countNodes(root.left) + countNodes(root.right);
    17. };