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. Coins in a Line II

    There are n coins with different value in a line. Two players take turns to take one or two coins fr ...

  2. 使用$.getJSON("xx.json" ,function(data){ console.logy(data); })在本地获取 json数据

    使用 $.getJSON() 在本地获取 json数据  默认在谷歌,火狐等其他浏览器,不允许在本地获取服务器数据 所以不能在本地中使用 但是可以IE 11 中又可以使用,所以,测试时建议在IE浏览器 ...

  3. 使用JS动态操作css的集中方法

    内联样式 在咱们深入一些复杂的知识之前,先回来顾一下一些基础知识.例如,咱们可以通过修改它的.style属性来编辑给定的HTMLElement的内联样式. const el = document.cr ...

  4. 前端开发规范:命名规范、HTML 规范、CSS 规范、JavaScript 规范

    一个好的程序员肯定是要能书写可维护的代码,而不是一次性的代码,怎么能让团队当中其他人甚至一段时间时候你再看你某个时候写的代码也能看懂呢,这就需要规范你的代码了.我是有一点强迫症的人,上周我们后端给我了 ...

  5. PHP基础语法之 三元运算符和其它运算符

    三元运算符和其它运算符 此外还有一些特殊的运算符和符号,我们再来进行讲解.可能以后我们需要用到.直线电机选型 符号 说明 $x? 真代码段:假代码段 判断是否为真假 ? 真情况 : 假情况; ``(反 ...

  6. 第一章使用JSP/Server技术开发新闻发布系统第一章动态网页开发基础

      一:为什么需要动态网页    由于静态网页的内容是固定的,不能提供个性化和定制化得服务,使用动态网页可真正地与用户实现互动. 二:什么是动态网页  ①:动态网页是指在服务器端运行的,使用程序语言设 ...

  7. Mxnet框架搭建

    Mxnet框架搭建 小书匠 kindle  Mxnet是亚马逊开发的深度学习框架,和谷歌Tensorflow是同类型的框架. 1.安装Mxnet 这里只展示在线安装,源码编译安装等不演示:GPU安装与 ...

  8. mysql的存储引擎与锁

    一.背景知识 1.锁是计算机协调多个进程或线程并发访问某一资源的机制. A.锁分类. | 共享锁(读锁):在锁定期间,多个用户可以读取同一个资源,读取过程中数据不会发生变化. | 排他锁(写锁):在锁 ...

  9. webpack-merge

    配置分离 随着我们业务逻辑的增多,图片.字体.css.ES6以及CSS预处理器和后处理器逐渐的加入到我们的项目中来,进而导致配置文件的增多,使得配置文件书写起来比较繁琐,更严重者(书写特定文件的位置会 ...

  10. Concurrent初探 --- Atomic 无锁

    一.CAS算法 Compare And Swap,CAS算法的过程是这样:它包含3个参数CAS(V,E,N).V表示要更新的变量,E表示预期值,N表示新值.仅当V值等于E值时,才会将V的值设为N,如果 ...