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
HashMapto 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 ...
随机推荐
- sqlserver建表及注释
--**********************************************创建表************************************************* ...
- Kubernetes学习之路(28)之镜像仓库Harbor部署
Harbor的部署 官方文档 Harbor有两种安装的方式: 在线安装:直接从Docker Hub下载Harbor的镜像,并启动. 离线安装:在官网上下载离线安装包其地址为:https://githu ...
- Linux Mysql创建新用户并允许远程连接
第一步 登陆mysql: mysql-u 数据库用户名 -h 数据库IP -p 根据提示 输入数据库密码 第二步: GRANT ALL PRIVILEGES ON *.* TO '自定义用户名'@'% ...
- Mac Docker安装Redis4.0
mkdir redis 在~目录下创建redis目录 docker run -d -p 6379:6379 -v $PWD/redis:/data -d --name redis4.0 redis:4 ...
- (转)虚拟文件系统(VFS)浅析
http://www.cnblogs.com/zsw-1993/p/5048144.html 在我看来, "虚拟"二字主要有两层含义: 1, 在同一个目录结构中, 可以挂载着若干种 ...
- hihocoder#1046: K个串
[传送门] 这种区间内相同数字只能被统计一次/只有区间内数字都不相同才对答案有贡献的题都可以用扫描线扫右端点,表示当前区间右端点为$r$.然后当前线段树/树状数组维护区间左端点为$[1,r)$时对应的 ...
- Codeforces 484 E. Sign on Fence
[传送门] 题意就是给一排围栏,每个围栏都有一个高度,查询区间$\left[l, r\right]$之间长度为$w$的子区间的最小高度的最大值.首先,这个最大值肯定是这个区间里的围栏的某个高度,如果是 ...
- 判断json对象是否在数组中
// 判断对象是否在数组中function objinArrar(check,param){ var isExisted = false; var index = -1; for(var i=0;i& ...
- 牛客练习赛55 E 树
题目链接: 题意:给出n个点,n-1条边求任意两个点的距离平方的和 解法: f[i]表示这个点的高度 sz[i]表示这个子树的大小 szz[i]表示这个这个子树大小的平方 sum[i]表示这个子树所有 ...
- [教程]Ubuntu16.04安装TeX Live
[教程]Ubuntu16.04安装TeX Live step 1 戳这里下载镜像 (只需要下载texlive.iso) 在终端输入 sudo apt-get install perl-tk step ...