题目:

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. vultr系统重建

    vultr服务器重启后ssh连接不上,view console只能guest登录,遍寻方法,无解,无奈重装系统. 服务器地址:Los Angeles 系统: Ubuntu 14.04 x64 用户:r ...

  2. 异常详细信息: System.ComponentModel.Win32Exception: 信号灯超时时间已到

  3. 微信小程序中使用echarts

    一.效果图 二.代码 import * as echarts from '../../component/ec-canvas/echarts'; const app = getApp(); var x ...

  4. iView webapp / Mint UI / MUI [前端UI]

    前端UI iView webapp一套高质量的 微信小程序 UI 组件库 https://weapp.iviewui.com/?from=iview Mint UI 基于 Vue.js 的移动端组件库 ...

  5. print keys %map_function 输出 散列的值: OK_funcsplit_funcpackage_VAR

    my %map_function = (     88     "OK_func" => "open_statement",     89     &qu ...

  6. xshell连接不了虚拟机处理方法(错误提示:Connection closing...Socket close.Connection closed by foreign host.Disconnected from remote host(localhost) at 08:47:23.)

    一.问题描述:xshell连接不了虚拟机,出现错误提示:Connection closing...Socket close.Connection closed by foreign host.Disc ...

  7. MySQL-----多对多

    多对多: 示例1: 用户表和相亲记录表 用户表 用户id 用户名                  性别 1 George  男 2 Elizabeth 女 3 Bruce 男 4 Catherine ...

  8. 关于使用mongodb中遇到的时间戳雷同的问题

    文不对题,实际上不是时间戳,而是我们使用js取当前毫秒数,将他看为时间戳,每次updata的时候,获取当前毫秒数,把它当做create_time的默认值,自动添加到我们的数据库中,数据模型如下 开始的 ...

  9. Mac os安装MySQL数据库,系统提示mysql: command not found该怎么办

    当我们安装好MySQL后,在终端输入mysql命令,发现并不能看到自己安装的数据库,这是因为你没有配置环境变量. 在os系统中安装MySQL数据库默认保存在/usr/local/mysql 那么我们应 ...

  10. 100ns周期200ns的正脉冲波形形状描述

    用FPGA控制16245产生正脉冲,正脉冲的形状是上升会有过冲现象,下降会有震荡产生,做了个实验室,用同轴电缆承载这种信号,在同轴电缆的末端会产生的波形是标准的衰减震荡. (简单的入pcb,经过继电器 ...