742. Closest Leaf in a Binary Tree

来源:互联网 发布:新加坡莱佛士书院知乎 编辑:程序博客网 时间:2024/06/10 11:03
Given a binary tree where every node has a unique value, and a target key k, find the value of the closest leaf node to target k in the tree.Here, closest to a leaf means the least number of edges travelled on the binary tree to reach any leaf of the tree. Also, a node is called a leaf if it has no children.In the following examples, the input tree is represented in flattened form row by row. The actual root tree given will be a TreeNode object.Example 1:Input:root = [1, 3, 2], k = 1Diagram of binary tree:          1         / \        3   2Output: 2 (or 3)Explanation: Either 2 or 3 is the closest leaf node to the target of 1.Example 2:Input:root = [1], k = 1Output: 1Explanation: The closest leaf node is the root node itself.Example 3:Input:root = [1,2,3,4,null,null,null,5,null,6], k = 2Diagram of binary tree:             1            / \           2   3          /         4        /       5      /     6Output: 3Explanation: The leaf node with value 3 (and not the leaf node with value 6) is closest to the node with value 2.Note:root represents a binary tree with at least 1 node and at most 1000 nodes.Every node has a unique node.val in range [1, 1000].There exists some node in the given binary tree for which node.val == k.

这道题目思路比较简单,将目标节点的路径求出来,然后求每个叶子节点到该目标节点的距离,选取距离最近的节点即可。、

/** * 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:    int min = 1000;    int val = 0;    bool findTargetNode(TreeNode * root,int key,vector<int> &distance){        if(root == NULL){            return false;        }        if(root->val == key){            return true;        }        if(root->left){            distance.push_back(0);            if(findTargetNode(root->left,key,distance)){                return true;            }            distance.pop_back();        }        if(root->right){            distance.push_back(1);            if(findTargetNode(root->right,key,distance)){                return true;            }            distance.pop_back();        }        return false;    }    int calculateDistance(vector<int> &path,vector<int> &distance){        int i = 0;        for(i = 0;i < path.size()&&i < distance.size();++i){            if(path[i] != distance[i]){                break;            }        }        return path.size() + distance.size()-2*i;    }    bool findShortDistance(TreeNode * root,vector<int> & path,vector<int> & distance){        if(root == NULL){            return false;        }        /*this node is leaf node*/        if(root->left == NULL&&root->right == NULL){            int dis = calculateDistance(path,distance);            if(dis<min){                min = dis;                val = root->val;            }        }        if(root->left){            distance.push_back(0);            findShortDistance(root->left,path,distance);            distance.pop_back();        }        if(root->right){            distance.push_back(1);            findShortDistance(root->right,path,distance);            distance.pop_back();        }        return true;    }    int findClosestLeaf(TreeNode* root, int k) {        vector<int> path;        vector<int> distance;        /*initial*/        findTargetNode(root,k,path);        /*debug*/        for(int i = 0; i < path.size();++i){            cout<<path[i]<<endl;        }        if(root == NULL){            return 0;        }        findShortDistance(root,path,distance);        return this->val;    }};