原题链接在这里:https://leetcode.com/problems/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 nearest leaf node to target k in the tree.

Here, nearest 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 nearest leaf node to the target of 1.

Example 2:

Input:
root = [1], k = 1
Output: 1 Explanation: The nearest 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 nearest to the node with value 2.

Note:

  1. root represents a binary tree with at least 1 node and at most 1000 nodes.
  2. Every node has a unique node.val in range [1, 1000].
  3. There exists some node in the given binary tree for which node.val == k.

题解:

First do DFS, find the TreeNode whose value is k, mark as kNode. At the same time, update HashMap with child->parent relationship.

Then do BFS from kNode, if any of left child, right child and parent is not null, put it into the queue. The first met leaf node is the nearest leaf node to kNode.

Time Complexity: O(n).

Space: O(n).

AC Java:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
TreeNode target; public int findClosestLeaf(TreeNode root, int k) {
if(root == null){
return -1;
} HashMap<TreeNode, TreeNode> nodeToPar = new HashMap<>();
dfs(root, null, nodeToPar, k); if(target == null){
return -1;
} LinkedList<TreeNode> que = new LinkedList<>();
HashSet<Integer> visited = new HashSet<>();
que.add(target);
visited.add(k); while(!que.isEmpty()){
TreeNode cur = que.poll();
if(cur.left == null && cur.right == null){
return cur.val;
} if(cur.left != null && !visited.contains(cur.left.val)){
que.add(cur.left);
visited.add(cur.left.val);
} if(cur.right != null && !visited.contains(cur.right.val)){
que.add(cur.right);
visited.add(cur.right.val);
} if(nodeToPar.containsKey(cur) && !visited.contains(nodeToPar.get(cur).val)){
que.add(nodeToPar.get(cur));
visited.add(nodeToPar.get(cur).val);
}
} return -1;
} private void dfs(TreeNode root, TreeNode parent, HashMap<TreeNode, TreeNode> nodeToPar, int k){
if(root == null){
return;
} if(parent != null){
nodeToPar.put(root, parent);
} if(root.val == k){
target = root;
} dfs(root.left, root, nodeToPar, k);
dfs(root.right, root, nodeToPar, k);
}
}

LeetCode 742. Closest Leaf in a Binary Tree的更多相关文章

  1. 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 ...

  2. [LeetCode] 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 n ...

  3. Leetcode: 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 n ...

  4. 【一天一道LeetCode】#104. Maximum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  5. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  6. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  7. 【LeetCode OJ】Minimum Depth of Binary Tree

    Problem Link: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ To find the minimum dept ...

  8. 【一天一道LeetCode】#111. Minimum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

随机推荐

  1. 去除Chrome“请停用以开发者模式运行的扩展程序”提示

    将version.dll放在chrome同级目录,重启浏览器( 79.0.3945.79版本后已失效)                    

  2. SAS学习笔记1

    数据采样 简单随机抽样,从sashelp数据集中air数据文件中选取30个数 数据探索 数字特征的探索:均值.频数.最大值.最小值.众数.中位数.方差.标准差 数字分布的探索:是否服从正态分布 连续型 ...

  3. 计算几何-凸包算法 Python实现与Matlab动画演示

    凸包算法是计算几何中的最经典问题之一了.给定一个点集,计算其凸包.凸包是什么就不罗嗦了 本文给出了<计算几何——算法与应用>中一书所列凸包算法的Python实现和Matlab实现,并给出了 ...

  4. golang学习笔记 ---常用第三方包

    包的介绍 包类似Java中概念,jar是源代码管理,分发的最小单位. 目前多数包来自 Github官方包来自 golang.org/x/... 可以在如下网址查询到高频使用的第三方包清单https:/ ...

  5. CentOS7 安装 Docker、最佳Docker学习文档

    目录 一.Docker支持 二.安装Docker -1.在新主机上首次安装Docker CE之前,需要设置Docker存储库.之后,就可以从存储库安装和更新Docker. 0.卸载旧版 1.正式安装 ...

  6. python 3.url了解与基础使用

    URL使用 视图: 我们运行项目在网页上查看到的我们称之为视图 视图一般在views.py下编辑 它的第一个参数永远都是request,通过它请求一些数据返回给网页给我们查看. 视图函数的返回结果必须 ...

  7. 使pre的内容自动换行(转)小知识

    <pre> 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体. <pre> 标签的一个常见应用就是用来表示计算机的源代码 ...

  8. BFC 到底是什么?

    MDN 对 BFC 的描述: 块格式化上下文(Block Formatting Context,BFC) 是Web页面的可视化CSS渲染的一部分,是布局过程中生成块级盒子的区域,也是浮动元素与其他元素 ...

  9. Node中require第三方模块的规则

    Node.js中使用CommonJs模块化机制,通过npm下载的第三方包,我们在项目中引入第三方包都是:let xx = require('第三方包名'),究竟require方法加载第三方包的原理机制 ...

  10. PHP使用Redis的Pub/Sub(发布订阅)命令

    1.概念 名称 含义 channel 频道:生产者和消费者直接操作的对象 publish 生产者:向channel发送消息 subscribe 消费者:订阅一个或多个channel psubscrib ...