Tree Operations 打印出有向图中的环
题目:
You are given a binary tree with unique integer values on each node. However, the child pointers on each node may point to any other node in the tree including itself, introducing cycles into the binary tree. A cycle is defined
when you can traverse back to the same node by following its descendants. Write a function that takes in the root node of the tree and prints out the cycles, if any, in the binary tree. The only operations available on each node are node.left (returns another
Node or null), node.right, and node.value (returns the integer value of the node). Pseudocode is fine.
即找出有向图中的环(loop或者cycle),而且所有打印出来。
Example: http://i.imgur.com/7S5fZe5.png

cycles: [1, 2, 4], [5], [3,6]
解答:
import java.util.ArrayList;
public class Graph {
enum VertexState {
White, Gray, Black
}
public static void main(String[] args) {
Node node = new Node(0);
node.color = VertexState.White;
Node left = new Node(1);
left.color = VertexState.White;
node.left = left;
Node right = new Node(2);
right.color = VertexState.White;
Node rightright = new Node(3);
node.right = right;
left.left = node;
right.right = rightright;
rightright.right = node;
ArrayList<Node> list = new ArrayList<Node>();
ArrayList<ArrayList<Node>> ret = new ArrayList<ArrayList<Node>>();
rec(node, list, ret);
System.out.println(ret);
}
public static void rec(Node node, ArrayList<Node> list, ArrayList<ArrayList<Node>> ret) {
if(node.color == VertexState.Gray) {
ret.add(new ArrayList<Node>(list));
return;
}
node.color = VertexState.Gray;
list.add(node);
if(node.left != null && node.left.color != VertexState.Black) {
rec(node.left, list, ret);
}
if(node.right != null && node.right.color != VertexState.Black) {
rec(node.right, list, ret);
}
list.remove(list.size()-1);
node.color = VertexState.Black;
}
public static class Node {
int val;
Node left;
Node right;
VertexState color;
public Node(int val_) {
val = val_;
}
@Override
public String toString() {
return this.val + "";
}
}
}
Tree Operations 打印出有向图中的环的更多相关文章
- [leetcode/lintcode 题解] 谷歌面试题:找出有向图中的弱连通分量
请找出有向图中弱连通分量.图中的每个节点包含 1 个标签和1 个相邻节点列表.(有向图的弱连通分量是任意两点均有有向边相连的极大子图) 将连通分量内的元素升序排列. 在线评测地址:https://ww ...
- iOS 打印出视图中全部的子视图的名称
使用递归: - (void)listSubviewsOfView:(UIView *)view { // Get the subviews of the view NSArray *subviews ...
- <数据结构>XDOJ323.判断有向图中是否有环
问题与解答 问题描述 判断有向图中是否有环. 输入格式 输入数据第一行是一个正整数,表示n个有向图,其余数据分成n组,每组第一个为一个整数,表示图中的顶点个数n,顶点数不超过100,之后为有向图的邻接 ...
- 在C++中打印出变量的方法
在C++中只能显示出字符串,而如果要想打印出其他类型的变量,则只能将其先转换为字符串类型. 例如:想打印出int型变量value的值 int value; 则需: char str[1];//定义一 ...
- hdu3342-判断有向图中是否存在(至少)3元环或回路-拓扑排序
一:题目大意: 给你一个关系图,判断是否合法, 每个人都有师父和徒弟,可以有很多个: 不合法: 1) . 互为师徒:(有回路) 2) .你的师父是你徒弟的徒弟,或者说你的徒弟是你师父的 ...
- 黑马基础阶段测试题:定义一个int类型的数组,数组中元素为{5,7,3,9,4}。求出数组中的最小值,并判断最小值是否为偶数,如果是偶数则输出“最小值为偶数”,如果不是偶数则输出“最小值为奇数”。打印如下:
package com.swift; import java.util.Arrays; public class ArrayTest { public static void main(String[ ...
- java算法面试题:从类似如下的文本文件中读取出所有的姓名,并打印出重复的姓名和重复的次数,并按重复次数排序 ;读取docx 读取doc 使用poi 相关jar包提集提供下载
从类似如下的文本文件中读取出所有的姓名,并打印出重复的姓名和重复的次数,并按重复次数排序 1,张三,28 2,李四,35 3,张三,28 4,王五,35 5,张三,28 6,李四,35 7,赵六,28 ...
- java-得到字符串中出现次数最最多的字符,并打印出字符以及出现次数
最近面试总被面试到,整理出几种方式(有参考别人的部分) /** * java一个字符串中出现次数最多的字符以及次数 * @param args */ public static void main(S ...
- LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)
和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...
随机推荐
- vultr服务器L2TP搭建
前期准备,购买外服,选择vultr服务商,可选择洛杉矶的,系统为Ubuntu 14.04 x64 一.安装L2TP/IPSec wget --no-check-certificate https:// ...
- 【转】UpdateData()函数
一.总结UpdateData()函数 UpdateData(true);//用于将屏幕上控件中的数据交换到变量中. UpdateData(false);//用于将数据在屏幕中对应控件中显示出来. 当你 ...
- vue工厂化完整项目目录
- rsync 同步多台服务器项目目录
server1:192.168.10.1server2:192.168.10.2实现server2实时同步server1的数据! ###############server1############# ...
- hdfs深入:10、hdfs的javaAPI操作
/** * 递归遍历hdfs中所有的文件路径 */ @Test public void getAllHdfsFilePath() throws URISyntaxException, IOExcept ...
- 简单的学生选课系统——基于Servlet+Ajax
以前挖的坑,早晚要往里掉.基础太薄弱,要恶补.在此程序前,我还对Servlet没有一个清晰的概念:一周时间写好此程序之后,对Servlet的理解清晰许多. 这周一直在恶补Spring,今天正好完成了S ...
- [Python3网络爬虫开发实战] 1.6.1-Flask的安装
Flask是一个轻量级的Web服务程序,它简单.易用.灵活,这里主要用来做一些API服务. 1. 相关链接 GitHub:https://github.com/pallets/flask 官方文档:h ...
- Kali Linux 下载、引导、安装
下载卡莉 Linux 官方镜像: https://www.kali.org/downloads/ 官方虚拟机镜像: https://www.offensive-security.com/kali-li ...
- super在python中有什么用
所属网站分类: python高级 > 面向对象 作者:阿里妈妈 链接:http://www.pythonheidong.com/blog/article/74/ 来源:python黑洞网 有什么 ...
- ELK搭建过程中出现的问题与解决方法汇总
搭建过程中出现的问题 elasticsearch启动过程中报错[1] ERROR: [1] bootstrap checks failed [1]: the default discovery set ...