原题链接在这里: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. AtCoder-abc147 (题解)

    A - Blackjack (水题) 题目链接 大致思路: 水题 B - Palindrome-philia (水题) 题目链接 大致思路: 由于整个串是回文串,只要判断前一半和后一半有多少个不同即可 ...

  2. for循环与if条件语句的复习运用

    鉴于前面学了不少基础了,今天没有学新的内容.boyfriend给我出了几道简单的题目,慢慢的进步中. 1.# 计算1-100之间所有偶数的和 def sum(): sumone = 0 for i i ...

  3. C# vb .net实现棕褐色效果特效滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的棕褐色效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一 ...

  4. 关于AWK的10个经典案例

    awk是Linux系统下一个处理文本的编程语言工具,能用简短的程序处理标准输入或文件.数据排序.计算以及生成报表等等,应用非常广泛.基本的命令语法:awk option 'pattern {actio ...

  5. 如何删除mysql注释

    Linux命令删除注释 先把库表导出成一个.sql文件,然后使用sed命令删除注释.此种适用于mysql端口不开外网的情况. $ cat create_table.sql create table t ...

  6. Oracle PLSQL数据导出csv的案例

    之前项目运维人员碰到一个问题,需要写一个存储过程,把数据导出为csv文件,查了一些资料,帮他写成了一个PLSQL,今天拿出来分享一下,不足之处,欢迎指教. 数据背景:  用到两张表,一张存放单位组织名 ...

  7. 全选全不选案例table表格

    全选全不选案例table表格 案例一纯table表格 <table class="table table-bordered"> <thead class=&quo ...

  8. v8--sort 方法 源码 (1) 插入排序法

    v8--sort方法源码中对于长度较短的数组使用的是插入排序法. 部分源码: function InsertionSort(a, from, to) { for (var i = from + 1; ...

  9. 单词dyamaund钻石dyamaund英语

    dyamaund  英文词汇,中文翻译为金刚石的;镶钻;用钻石装饰 中文名:镶钻;钻石装饰 外文名:dyamaund 目录 释义 dyamaund 读音:[?da??m?nd, ?da?m?nd] ...

  10. Scala快速入门 - 基础语法篇

    本篇文章首发于头条号Scala快速入门 - 基础语法篇,欢迎关注我的头条号和微信公众号"大数据技术和人工智能"(微信搜索bigdata_ai_tech)获取更多干货,也欢迎关注我的 ...