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 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:
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.
Tree -----> Graph
- First, preform DFS on root in order to find the node whose val = k, at the meantime use
HashMap
to keep record of all back edges from child to parent; - Then perform BFS on this node to find the closest leaf node.
Note only the nodes visited in DFS are put into the backedgeMap, the others don't. This is fine, cause only from KNode to root this path, we need to check backMap.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int findClosestLeaf(TreeNode root, int k) {
HashMap<TreeNode, TreeNode> backMap = new HashMap<>(); // store all edges that trace node back to its parent
HashSet<TreeNode> visited = new HashSet<>(); // visited node in BFS
Queue<TreeNode> queue = new LinkedList<>(); // for BFS TreeNode KNode = dfs(root, k, backMap); // DFS: search for node whoes val == k queue.offer(KNode);
visited.add(KNode); while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
if (cur.left == null && cur.right == null) {
return cur.val;
}
if (cur.left != null && visited.add(cur.left)) {
queue.offer(cur.left);
}
if (cur.right != null && visited.add(cur.right)) {
queue.offer(cur.right);
}
if (backMap.containsKey(cur) && visited.add(backMap.get(cur))) {
queue.offer(backMap.get(cur));
}
} return -1;
} public TreeNode dfs(TreeNode cur, int k, HashMap<TreeNode, TreeNode> backMap) {
if (cur.val == k) {
return cur;
}
if (cur.left != null) {
TreeNode left = dfs(cur.left, k, backMap);
backMap.put(cur.left, cur);
if (left != null) return left;
}
if (cur.right != null) {
TreeNode right = dfs(cur.right, k, backMap);
backMap.put(cur.right, cur);
if (right != null) return right;
}
return null;
}
}
Leetcode: Closest Leaf in a Binary Tree的更多相关文章
- [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 742. Closest Leaf in a Binary Tree
原题链接在这里:https://leetcode.com/problems/closest-leaf-in-a-binary-tree/ 题目: Given a binary tree where e ...
- 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】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- LeetCode 104. Maximum Depth of Binary Tree (二叉树的最大深度)
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
随机推荐
- CentOS7- 配置阿里镜像源
CentOS7- 配置阿里镜像源 1. 安装wgetyum install -y wget 2. 用wget下载repo文件 输入命令wget http://mirrors.aliyun.com/re ...
- Mac pro操作快捷键
1. 在Finder顶部显示文件/文件夹全路径 终端里输入:defaults write com.apple.finder _FXShowPosixPathInTitle -bool TRUE;kil ...
- Linux命令head
1.命令简介 head (head) 用来显示档案的开头至标准输出中.如果指定了多于一个文件,在每一段输出前会给出文件名作为文件头.如果不指定文件,或者文件为"-",则从标准输入读 ...
- windows下端口占用解决办法,解决命令
1.netstat -aon|findstr "8183" 可以看到listener 对应的端口id 2.taskkill /pid {查询出来对应的id值} /f
- eval用法
在shell的学习中,我们会遇到这两种符号:反引号(` `)和$(),那么它们之间有什么区别和联系呢? 我们都知道在bash中,反引号和$()都是用来做命令替换的,命令替换就是用来重组命令行,先完成引 ...
- Linux上安装GO开发环境+第一个程序编译运行
首先官网下载包: 使用wget命令下载到自己的目录里 wget https://dl.google.com/go/go1.13.4.linux-amd64.tar.gz 解压: tar -xvf go ...
- kuangbin专题专题四 MPI Maelstrom POJ - 1502
题目链接:https://vjudge.net/problem/POJ-1502 dijkstra板子题,题目提供下三角情况,不包含正对角线,因为有题意都为0,处理好输入,就是一个很水的题. #inc ...
- Win10 家庭版 VMware 无法启动 解决办法
引发原因 最近更新了一个补丁 KB4524147 安装后会导致 VM 无法打开(如果你没有安装hyper-v的话) 解决方案 控制面板 -> 程序 -> 查看已安装的更新 -> 找到 ...
- Android AMS服务
继续来研究Android Framework层相关的一些东东,这里是以Android8.0版本的源码进行梳理的,关注的还是其核心流程,不是彻底分析,了解了核心流程是为了了期其大概的原理. Androi ...
- wordpress文章显示同一分类下的上一篇下一篇
我们在用wordpress开发网站的时候会在文章页中引入上一篇下一篇,但是发现新闻页的上下文章有可能是产品分类的post,这个就不太合理,如何显示同一分类下的上一篇下一篇文章呢?随ytkah一起来看看 ...