<p>You are given the <code>root</code> of a binary tree and an integer <code>distance</code>. A pair of two different <strong>leaf</strong> nodes of a binary tree is said to be good if the length of <strong>the shortest path</strong> between them is less than or equal to <code>distance</code>.</p>

<p>Return <em>the number of good leaf node pairs</em> in the tree.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/09/e1.jpg" style="width: 250px; height: 250px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,null,4], distance = 3
<strong>Output:</strong> 1
<strong>Explanation:</strong> The leaf nodes of the tree are 3 and 4 and the length of the shortest path between them is 3. This is the only good pair.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/09/e2.jpg" style="width: 250px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7], distance = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> The good pairs are [4,5] and [6,7] with shortest path = 2. The pair [4,6] is not good because the length of ther shortest path between them is 4.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> root = [7,1,4,6,null,5,3,null,null,null,null,null,2], distance = 3
<strong>Output:</strong> 1
<strong>Explanation:</strong> The only good pair is [2,5].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the <code>tree</code> is in the range <code>[1, 2<sup>10</sup>].</code></li>
	<li><code>1 &lt;= Node.val &lt;= 100</code></li>
	<li><code>1 &lt;= distance &lt;= 10</code></li>
</ul>

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private: 
    map<TreeNode*, TreeNode*> parents;
    vector<TreeNode*> leaves;
    set<TreeNode*> vis;

public:
    bool is_leaf(TreeNode* node)
    {
        return !node->left && !node->right;
    }

    bool visited(TreeNode* node)
    {
        return vis.find(node) != vis.end();
    }

    void get_parents_and_leaves(TreeNode* node) // does not assign parent to root!
    {
        if (is_leaf(node)) leaves.push_back(node);

        if (node->left)
        {
            parents[node->left] = node;
            get_parents_and_leaves(node->left);
        }
        if (node->right)
        {
            parents[node->right] = node;
            get_parents_and_leaves(node->right);
        }
    }

    int dfs(TreeNode* node, int distance, int k) // finds number of nodes at distance k from leaf
    {
        if (node == nullptr) return 0;

        vis.insert(node);
        if (is_leaf(node) && distance != k) return 1;
        if (distance == 0) return is_leaf(node);

        int left = 0, right = 0, up = 0;
        
        if (node->left && !visited(node->left))
        {
            left = dfs(node->left, distance - 1, k);
        }
        if (node->right && !visited(node->right))
        {
            right = dfs(node->right, distance - 1, k);
        }
        if (parents[node] && !visited(parents[node]))
        {
            up = dfs(parents[node], distance - 1, k);
        }
        return left + right + up;
    }

    int countPairs(TreeNode* root, int distance) 
    {
        get_parents_and_leaves(root);
        int k = distance;

        int count = 0;

        for (TreeNode* leaf : leaves)
        {
            count += dfs(leaf, k, k);
            vis.clear();
        }

        return count/2;

    }
};
75031