题目:

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. CAD参数绘制直径标注(com接口)

    主要用到函数说明: _DMxDrawX::DrawDimDiametric 绘制一个直径标注.详细说明如下: 参数 说明 DOUBLE dChordPointX 在被标注的曲线上的第一个点X值 DOU ...

  2. JS如何禁用浏览器的退格键

    <script type="text/javascript"> //处理键盘事件 禁止后退键(Backspace)密码或单行.多行文本框除外 function forb ...

  3. React入门介绍(1)-ReactDOM.render()等基础

    React入门介绍-ReactDOM.render()等基础 首先,React是一个用于构建用户界面的Javascript库,但Peact并不是一套完整的MVC或MVVM的框架,它仅涵盖V-view视 ...

  4. [Python3网络爬虫开发实战] 3.1.4-分析Robots协议

    利用urllib的robotparser模块,我们可以实现网站Robots协议的分析.本节中,我们来简单了解一下该模块的用法. 1. Robots协议 Robots协议也称作爬虫协议.机器人协议,它的 ...

  5. Buffer.isBuffer()详解

    Buffer.isBuffer(obj) obj {Object} 返回:{Boolean} 如果 obj 是一个 Buffer 则返回 true.

  6. centos相关

    查看虚拟机里的Centos7的IP:ip addr或者ifconfig  ---https://blog.csdn.net/dancheren/article/details/73611878 Cen ...

  7. 九度oj 题目1203:IP地址

    题目1203:IP地址 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3636 解决:1800 题目描述: 输入一个ip地址串,判断是否合法. 输入: 输入的第一行包括一个整数n(1< ...

  8. JavaEE JDBC PreparedStatement

    PreparedStatement @author ixenos PreparedStatement工作原理 注意:虽然mysql不支持PreparedStatement优化,但依然有预编译的实现! ...

  9. [HDU2196]Computer(DP)

    传送门 题意 给出一棵树,求离每个节点最远的点的距离 思路 对于我这种菜鸡,真是难啊. 每个点的距离它最远的点,除了在它子树中的,还有在它子树之外的,所以这几个状态都得表示出来. 我们能够很简单的求出 ...

  10. BBS+Blog项目流程及补充知识点

    项目流程: 1. 产品需求 (1)基于用户认证组件和Ajax实现登陆验证(图片验证码) (2)基于forms组件和Ajax实现注册功能 (3)设计系统首页(文章列表渲染) (4)设计个人站点页面 (5 ...