Wednesday, April 28, 2021

863. All Nodes Distance K in Binary Tree

 We are given a binary tree (with root node root), a target node, and an integer value K.

Return a list of the values of all nodes that have a distance K from the targetnode.  The answer can be returned in any order.

 

    Example 1:

    Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, K = 2
    
    Output: [7,4,1]
    
    Explanation: 
    The nodes that are a distance 2 from the target node (with value 5)
    have values 7, 4, and 1.
    
    
    
    Note that the inputs "root" and "target" are actually TreeNodes.
    The descriptions of the inputs above are just serializations of these objects.
    

     

    Note:

    1. The given tree is non-empty.
    2. Each node in the tree has unique values 0 <= node.val <= 500.
    3. The target node is a node in the tree.
    4. 0 <= K <= 1000.

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        
        vector<int> distanceK(TreeNode* root, TreeNode* target, int K) {
            vector<int> ans;
            if (root == NULL || target == NULL) {
                return ans;
            }
            if (K == 0) {
                return {target->val};
            }
            // Prepare parentMap
            unordered_map<TreeNode*, TreeNode*> mp;
            PrepareParent(root, NULL, mp);
            
            // BFS
            return BFS(target, mp, K);
        }
        
        void PrepareParent(TreeNode* root, TreeNode *parent, unordered_map<TreeNode*, TreeNode*>& mp) {
            if (root == NULL) {
                return;
            }
            mp[root] = parent;
            PrepareParent(root -> left, root, mp);
            PrepareParent(root -> right, root, mp);
        }
        
        void checkNode(TreeNode *node, queue<TreeNode *>& q, unordered_set<TreeNode *>& visited, vector<int>& ans, int level, int k) {
            if (node != NULL && visited.count(node) == 0) {
                visited.insert(node);
                if (level == k - 1) {
                    ans.push_back(node -> val);
                } else {
                    q.push(node);
                }
            }
        }
        
        vector<int> BFS(TreeNode* node, unordered_map<TreeNode*, TreeNode*>& mp, int k) {
            vector<int> ans;
            unordered_set<TreeNode *> visited;
            queue<TreeNode *> q;
            q.push(node);
            visited.insert(node);
            int level = 0;
            while (!q.empty()) {
                int size = q.size();
                for (int i = 0; i < size; i++) {
                    TreeNode *node = q.front(); q.pop();
                    // All neighbors (both child + parent)
                    TreeNode * left = node -> left;
                    checkNode(left, q, visited, ans, level, k);
                    TreeNode * right = node -> right;
                    checkNode(right, q, visited, ans, level, k);
                    TreeNode * parent = mp[node];
                    checkNode(parent, q, visited, ans, level, k);
                }
                level++;
            }
            return ans;
        }

        // DFS one is shorter too
        /*
        
        class Solution {
    public:
        vector<int> ans;   
        map<TreeNode*, TreeNode*> parent;  // son=>parent  
        set<TreeNode*> visit;    //record visied node
        
        void findParent(TreeNode* node){
            if(!node ) return;
            if( node->left ){
                parent[node->left] = node;
                findParent(node->left);
            }
            if( node->right){
                parent[node->right] = node;
                findParent(node->right);
            }
        }
        
        vector<int> distanceK(TreeNode* root, TreeNode* target, int K) {
            if( !root ) return {};
            
            findParent(root);
            dfs(target, K );
            return ans;
        }
        void dfs( TreeNode* node, int K){
            if( visit.find(node) != visit.end() )
                return;
            visit.insert(node);
            if( K == 0 ){
                ans.push_back(node->val);
                return;
            }
            if( node->left ){
                dfs(node->left, K-1);
            }
            if( node->right){
                dfs(node->right, K-1);
            }
            TreeNode* p = parent[node];
            if( p )
                dfs(p, K-1);
        }
    };
        
        */

    };

    No comments:

    Post a Comment