LeetCode 742. Closest Leaf in a Binary Tree
原题链接在这里: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:
rootrepresents a binary tree with at least1node and at most1000nodes.- Every node has a unique
node.valin range[1, 1000]. - 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的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- 【一天一道LeetCode】#104. Maximum Depth of Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【LeetCode OJ】Minimum Depth of Binary Tree
Problem Link: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ To find the minimum dept ...
- 【一天一道LeetCode】#111. Minimum Depth of Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
随机推荐
- Cpp_Primer_4th_Edition-source-code
Cpp_Primer_4th_Edition-source-code 根据书上的去找,官网已经找不到了,毕竟第6版都已经出来了.不过有的朋友用的还是第4版,我的纸质书是第5版,pdf是第4版,都有在看 ...
- redis源码分析(五)--cluster(集群)结构
Redis集群 Redis支持集群模式,集群中可以存在多个master,每个master又可以拥有多个slave.数据根据关键字映射到不同的slot,每一个master负责一部分的slots,数据被存 ...
- t100 debug常用指令
1. r.d 程式代號 Find (Ctrl+F) => Find Next(F3). 設中斷點(Toggle):Double Click => Run/Continue the ...
- global position
观察, GestureDetector( child: CustomPaint(painter: StudyPaint(points: _points)), onPanEnd: (DragEndDet ...
- 手写MVC框架(二)-代码实现和使用示例
--------上一篇:手写MVC框架(一)-再出发----- 背景 书接上文,之前整理了实现MVC框架需要写哪些东西.这周粗看了一下,感觉也没多少工作量,所以就计划一天时间来完成.周末的时间,哪会那 ...
- Flask 基础总结回顾
1.Flask Request # from flask import request request.form # 获取FormData中的数据 request.args # 获取URL中的数据 r ...
- Requirements management in confluence
https://ja.confluence.atlassian.com/doc/blog/2015/08/how-to-document-product-requirements-in-conflue ...
- Hive架构分析
一.Hive三种设计模式 1.默认配置[使用Netty存储元数据] 2.mysql[使用mysql存储元数据] 3.配置Thrift[使用mysql存储元数据] 二.执行步骤 三.scala访问Hiv ...
- 【DATAGUARD】物理dg配置客户端无缝切换 (八.1)--Data Guard Broker 的配置
[DATAGUARD]物理dg配置客户端无缝切换 (八.1)--Data Guard Broker 的配置 一.1 BLOG文档结构图 一.2 前言部分 一.2.1 导读 各位技 ...
- 【故障处理】ORA-12162 错误的处理
[故障处理]ORA-12162: TNS:net service name is incorrectly specified 一.1 场景 今天拿到一个新的环境,可是执行sqlplus / as s ...