742. Closest Leaf in a Binary Tree

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 = 1
Diagram of binary tree:
          1
         / \
        3   2

Output: 2 (or 3)

Explanation: Either 2 or 3 is the closest leaf node to the target of 1.
Example 2:

Input:
root = [1], k = 1
Output: 1

Explanation: The closest leaf node is the root node itself.
Example 3:

Input:
root = [1,2,3,4,null,null,null,5,null,6], k = 2
Diagram of binary tree:
             1
            / \
           2   3
          /
         4
        /
       5
      /
     6

Output: 3
Explanation: 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;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值