Skip to content

Latest commit

 

History

History
executable file
·
110 lines (95 loc) · 3.16 KB

0863. All Nodes Distance K in Binary Tree.md

File metadata and controls

executable file
·
110 lines (95 loc) · 3.16 KB

0863. All Nodes Distance K in Binary Tree, medium, , freq: 38%, acceptance: 48.8%

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 target node. 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:

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

// 4ms, 94%
/*
    find the target first, then check two children, then return K - 1 for parent to check
*/
/**
 * 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 {
    int find(TreeNode* root, TreeNode* target,
                   int n, int K, vector<int>& res) {
        if (!root) return -1;
        // if need to find target
        if (n == K) {
            if (root->val == target->val) {
                if (K == 0) {
                    res.push_back(target->val);
                    return -1;
                }
                if (root->left) {
                    find(root->left, target, n - 1, K, res);
                }
                if (root->right) {
                    find(root->right, target, n - 1, K, res);
                }
                return K - 1; // look up
            } else {
                if (root->left) {
                    int r = find(root->left, target, n, K, res);
                    if (r >= 0) {
                        if (r == 0) {
                            res.push_back(root->val);
                            return -1;
                        }
                        find(root->right, target, r - 1, K, res);
                        return r - 1;
                    }
                }
                if (root->right) {
                    int r = find(root->right, target, n, K, res);
                    if (r >= 0) {
                        if (r == 0) {
                            res.push_back(root->val);
                            return -1;
                        }
                        find(root->left, target, r - 1, K, res);
                        return r - 1;
                    }
                }
            }
        } else {
            if (n == 0) {
                res.push_back(root->val);
                return -1;
            }
            if (root->left) {
                find(root->left, target, n - 1, K, res);
            }
            if (root->right) {
                find(root->right, target, n - 1, K, res);
            }
            return -1;
        }
        return -1;
     }
public:
    vector<int> distanceK(TreeNode* root, TreeNode* target, int K) {
        vector<int> res;
        find(root, target, K, K, res);
        return res;
    }
};