[CareerCup] Single Valid Tree
https://www.careercup.com/question?id=5103530547347456
Given a list of nodes, each with a left child and a right child (they can be null), determine if all nodes belong in a single valid binary tree. The root is not given.
Li家
The solution can be designed with the following idea which runs in multiple passes:
Pass1: Read every node and for each node remember what other nodes are pointing to it using additional data structures
Pass2: Read every node to find the following:
a. Nodes who are pointed to by 0 other nodes ==> These are potential roots
b. Nodes who are pointed to by 1 nodes
c. Nodes who are pointed to by > 1 nodes
We have a valid binary tree iff:
1. The number of nodes whom nobody points to is 1 and that is the root
2. Every node is pointed to by at most one node
3. Starting with the root, and doing a DFS or a BFS covers all the nodes in the list
Java:
public boolean isValid(List<TreeNode> nodes){
HashSet<TreeNode> children = new HashSet<> ();
// child node only has one parent node
for (TreeNode node : nodes) {
if (node.left != null) {
if (!children.add(node.left)) return false ;
}
if (node.right != null) {
if (!children.add(node.right)) return false ;
}
}
TreeNode start = null ;
int count = 0 ;
for (TreeNode node : nodes) {
if (!children.contains(node)) {
start = node ;
count ++ ;
}
}
// only has one root node
if (count > 1) return false ;
// running bfs to make sure all nodes can be constructed as a binary tree
Queue<TreeNode> q = new LinkedList<> ();
q.add(start) ;
while (!q.isEmpty()) {
int size = q.size() ;
for (int i = 0 ; i < size ; ++i) {
TreeNode cur = q.poll() ;
if (cur.left != null) {
q.add(cur.left) ;
children.remove(cur.left) ;
}
if (cur.right != null) {
q.add(cur.right) ;
children.remove(cur.right) ;
}
}
}
return children.size() == 0 ;
}
类似题目:
[LeetCode] 261. Graph Valid Tree 图是否是树
[CareerCup] Single Valid Tree的更多相关文章
- [LeetCode] Graph Valid Tree 图验证树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- Graph Valid Tree
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- LeetCode Graph Valid Tree
原题链接在这里:https://leetcode.com/problems/graph-valid-tree/ 题目: Given n nodes labeled from 0 to n - 1 an ...
- Leetcode: Graph Valid Tree && Summary: Detect cycle in undirected graph
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- 261. Graph Valid Tree
题目: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nod ...
- [LeetCode#261] Graph Valid Tree
Problem: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair o ...
- [Locked] Graph Valid Tree
Graph Valid Tree Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is ...
- [Swift]LeetCode261.图验证树 $ Graph Valid Tree
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] 261. Graph Valid Tree _ Medium tag: BFS
Given n nodes labeled from 0 to n-1 and a list of undirected edges (each edge is a pair of nodes), w ...
随机推荐
- singleton单例模式小结
1.饿汉模式 public class SingletonEntity2 { // 在类加载的时候创建对象:饿汉模式 public static SingletonEntity2 obj = new ...
- Linux中drwxr-xr-x.的意思和权限
转载:https://blog.csdn.net/weixin_39209728/article/details/79729885 读(read),写(write),执行r(recute)简写即为(r ...
- 什么是 restful 规范?
一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件. 一.协议 API与用户的通信协议,总是使用HTTPs协议. 什么是https协议 二.域名 应该尽量将API部署在专用域名之 ...
- Tcl在Vivado中的应用
转载: https://blog.csdn.net/avrmcu1/article/details/43446529?utm_source=blogxgwz6
- JZOJ 5870 地图
直接解释题解,记录一下.
- 《论文翻译》Xception
目录 深度可分离网络-Xception 注释 1. 摘要 2. 介绍 3. Inception假设 4. 卷积和分离卷积之间的联系 4. 先验工作 5. Xception 架构 6. 个人理解 单词汇 ...
- python下载后出现python 已停止工作
背景: 在执行IDLE或者在terminal窗口执行 python命令时出现如下提示,修改了防火墙关闭也不行,找不到解决办法? 如图: [解决方案] 1.卸载重装python,确保python版本与系 ...
- ros topic 命令
#查看topic频率rostopic hz /xxx_imu_driver/imu #查看topic信息rostopic info /xxx_imu_driver/imu #查看topic数据rost ...
- CentOS7 升级Python2.x到3.x
CentOS 7 中默认安装了 Python,版本比较低(2.7.5),为了使用新版 3.x,需要对旧版本进行升级.由于很多基本的命令.软件包都依赖旧版本,比如:yum.所以,在更新 Python 时 ...
- Hadoop Aggregate Resource Allocation解释
1.在hadoop里面运行程序的时候,查看某个任务的具体信息如下: [hadoop@master monitor]$ yarn application -list 如上图,这里面的Aggregate ...