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的更多相关文章

  1. [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), ...

  2. 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), ...

  3. LeetCode Graph Valid Tree

    原题链接在这里:https://leetcode.com/problems/graph-valid-tree/ 题目: Given n nodes labeled from 0 to n - 1 an ...

  4. 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), ...

  5. 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 ...

  6. [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 ...

  7. [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 ...

  8. [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), ...

  9. [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 ...

随机推荐

  1. linux 安装MySql 5.7.20 操作步骤【亲测】

    一. #卸载系统自带的Mariadb[root@master ~]# rpm -qa|grep mariadbmariadb-libs-5.5.44-2.el7.centos.x86_64[root@ ...

  2. SQL数据库调优

    1.使用With As做数据库递归,调优树形表结构 例如:设计表结构简化如:ID.ParentID.Name:这里的ParentID就是这个表本身的某个ID WITH cte AS ( UNION A ...

  3. ElementUI 之 Message,自动弹出,信息不显示问题

    Vue.use() 这种写法可能会页面刷新时自动弹出 message,可将下图红框里换成 Vue.component(Message)

  4. 010_IAR安装

    链接:https://pan.baidu.com/s/14qZh1Gxl32dD2TWdjEYP7Q提取码:yj65 复制这段内容后打开百度网盘手机App,操作更方便哦 里面有安装说明 (一)编辑界面 ...

  5. 获取Druid连接池里当前连接数

    JdbcTemplate jdbcTemplate=(JdbcTemplate) SpringUtils.getBean("jdbcMysqlTemplate"); DruidDa ...

  6. epoll反应堆模型代码

    libevent函数库核心思想 /*** epoll_loop.c ***/ #include<stdio.h> #include<sys/epoll.h> #include& ...

  7. js new call apply bind 的 原理

    new new 做了什么事?1. 以 Object.protoype 为原型创建一个新对象 2. 以新对象为 this,执行函数的 [[call]] 3. 如果 [[call]] 的返回值是对象,那么 ...

  8. 关于解决ruby源码安装 gem install报错问题

    因做redis集群需要安装ruby,源码安装过后gem install redis安装redis接口报错 解决方案: 确保主机安装zlib,没有安装执行 yum -y install zlib zli ...

  9. 做reacat小项目的过程(我感觉适合那种刚刚接触react感觉很深奥的亲们,通过这个可以建立一个很垃圾的项目,入门吧,往深处就需要自己再看了)

    需求:做一个react框架的前端框架(包括路由,请求后端等),大概就是做一个左边导航右边显示组件页面的东西,ui为material-ui 环境: 软件:vscode 包含的知识点:使用路由来导航,使用 ...

  10. python2和python3区别

    字符编码: py3中默认字符编码是unicode:py2中默认字符编码是 ASCII,如果文件中出现了中文,需要在顶部加入coding声明#coding:utf8 让用户输入:py3中直接使用inpu ...