<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) {}
 * };
 */

// O(n * distance^2) time
class Solution1 {
public:
    int countPairs(TreeNode* root, int distance) {
        return f1(root, distance).first;
    }
private:
    // returns <count, map of <dist, number of leaf nodes at dist>>
    pair<int, unordered_map<int, int>> f1(TreeNode* root, int dist) {
        if(!root) return {0, {}};
        if(!root->left && !root->right) {
            return {0, {{0,1}}};
        }
        
        auto left = f1(root->left, dist);
        auto right = f1(root->right, dist);
        
        int count = left.first + right.first;
        for(auto& keyval1 : left.second) {
            for(auto& keyval2 : right.second) {
                if(1 + keyval1.first + 1 + keyval2.first <= dist) {
                    count += keyval1.second * keyval2.second;
                }
            }
        }
        
        unordered_map<int, int> leaves;
        for(auto& keyval : left.second) {
            if(1 + keyval.first < dist)
                leaves[1 + keyval.first] += keyval.second;
        }
        for(auto& keyval : right.second) {
            if(1 + keyval.first < dist)
                leaves[1 + keyval.first] += keyval.second;
        }
        
        return {count, leaves};
    }
};

// S2. Madel the tree as a graph, and find the number of pairable 
class Solution {
public:
    int countPairs(TreeNode* root, int distance) {
        unordered_set<TreeNode*> leaves;
        unordered_map<TreeNode*, TreeNode*> parent;
        parent[root] = nullptr;
        populate(root, leaves, parent);
        
        int count = 0;
        for(TreeNode* leaf : leaves) {
            unordered_set<TreeNode*> visited;
            dfs(leaf, visited, 0, distance, leaves, parent, count);
        }
        return count / 2;
    }
private:
    void dfs(TreeNode* node, unordered_set<TreeNode*>& visited, int d, 
             const int distance, unordered_set<TreeNode*>& leaves,
             unordered_map<TreeNode*, TreeNode*>& parent, int& count) {
        if(leaves.find(node) != leaves.end() && d > 0 && d <= distance) {
            count++;
        }
        if(d == distance) return; // branch pruning
        
        visited.insert(node);
        if(node->left && visited.find(node->left) == visited.end()) 
            dfs(node->left, visited, d+1, distance, leaves, parent, count);
        if(node->right && visited.find(node->right) == visited.end()) 
            dfs(node->right, visited, d+1, distance, leaves, parent, count);
        if(parent[node] != nullptr && visited.find(parent[node]) == visited.end())
            dfs(parent[node], visited, d+1, distance, leaves, parent, count);
    }
    
    void populate(TreeNode* root, unordered_set<TreeNode*>& leaves,
             unordered_map<TreeNode*, TreeNode*>& parent) {
        if(!root) return;
        if(!root->left && !root->right) {
            leaves.insert(root);
        } else {
            if(root->left) {
                parent[root->left] = root;
                populate(root->left, leaves, parent);
            }
            if(root->right) {
                parent[root->right] = root;
                populate(root->right, leaves, parent);
            }
        }
    }
};











61868