题目:

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 打印出有向图中的环的更多相关文章

  1. [leetcode/lintcode 题解] 谷歌面试题:找出有向图中的弱连通分量

    请找出有向图中弱连通分量.图中的每个节点包含 1 个标签和1 个相邻节点列表.(有向图的弱连通分量是任意两点均有有向边相连的极大子图) 将连通分量内的元素升序排列. 在线评测地址:https://ww ...

  2. iOS 打印出视图中全部的子视图的名称

    使用递归: - (void)listSubviewsOfView:(UIView *)view { // Get the subviews of the view NSArray *subviews ...

  3. <数据结构>XDOJ323.判断有向图中是否有环

    问题与解答 问题描述 判断有向图中是否有环. 输入格式 输入数据第一行是一个正整数,表示n个有向图,其余数据分成n组,每组第一个为一个整数,表示图中的顶点个数n,顶点数不超过100,之后为有向图的邻接 ...

  4. 在C++中打印出变量的方法

    在C++中只能显示出字符串,而如果要想打印出其他类型的变量,则只能将其先转换为字符串类型. 例如:想打印出int型变量value的值 int  value; 则需: char str[1];//定义一 ...

  5. hdu3342-判断有向图中是否存在(至少)3元环或回路-拓扑排序

    一:题目大意:   给你一个关系图,判断是否合法,    每个人都有师父和徒弟,可以有很多个:  不合法:  1) . 互为师徒:(有回路)  2) .你的师父是你徒弟的徒弟,或者说你的徒弟是你师父的 ...

  6. 黑马基础阶段测试题:定义一个int类型的数组,数组中元素为{5,7,3,9,4}。求出数组中的最小值,并判断最小值是否为偶数,如果是偶数则输出“最小值为偶数”,如果不是偶数则输出“最小值为奇数”。打印如下:

    package com.swift; import java.util.Arrays; public class ArrayTest { public static void main(String[ ...

  7. java算法面试题:从类似如下的文本文件中读取出所有的姓名,并打印出重复的姓名和重复的次数,并按重复次数排序 ;读取docx 读取doc 使用poi 相关jar包提集提供下载

    从类似如下的文本文件中读取出所有的姓名,并打印出重复的姓名和重复的次数,并按重复次数排序 1,张三,28 2,李四,35 3,张三,28 4,王五,35 5,张三,28 6,李四,35 7,赵六,28 ...

  8. java-得到字符串中出现次数最最多的字符,并打印出字符以及出现次数

    最近面试总被面试到,整理出几种方式(有参考别人的部分) /** * java一个字符串中出现次数最多的字符以及次数 * @param args */ public static void main(S ...

  9. LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)

    和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...

随机推荐

  1. [Windows Server 2008] 搭建数据云备份

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:如何搭建数 ...

  2. Codeforces_789C_(dp)

    C. Functions again time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. 网络基础编程_5.4聊天室-IOCP服务器

    聊天室-IOCP服务器 main 创建完成端口内核对象(CreateIoCompletionPort) 获取核心数并创建线程(GetSystemInfo + CreateThread) 创建套接字并绑 ...

  4. delphi 7 生成 调用 bat文件的exe文件

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  5. QTreeWidgetItem和QTreeWidgetItemIterator

    1.{ ui->treeWidget->setHeaderHidden(true); ui->treeWidget->clear(); QTreeWidgetItem *ima ...

  6. caffe这个c++工程的目录结构

    目录结构 caffe文件夹下主要文件: data 用于存放下载的训练数据 docs 帮助文档 example 一些代码样例 matlab MATLAB接口文件 python Python接口文件 mo ...

  7. eclipse包分层

    方法很简单,如下图所示: 1.点击项目栏窗口的右上角的倒三角 2.选择Pachage Presentation(包呈现) 3.选择Hierarchical(分层)

  8. elementui 后台管理系统遇到的问题(二) 树形控件 el-tree

    elementui中树形控件的使用 一.将后台返回的数据填充到前端控件中,需要注意的几点问题 (1).el-tree中需要绑定node-key='自定义的id名称' (2).在配置data中defau ...

  9. 安装配置elasticsearch、安装elasticsearch-analysis-ik插件、mysql导入数据到elasticsearch、安装yii2-elasticsearch及使用

    一.安装elasticsearch 获取elasticsearch的rpm:wget https://download.elastic.co/elasticsearch/release/org/ela ...

  10. Linux命令学习(6):paste合并几列文件

    如果我们有三个文件: $ cat name.txt #姓名文档 Kevin Mary Tom $ cat gender.txt #性别文档 M F M $ cat age.txt #年龄文档 我们想把 ...