zoukankan      html  css  js  c++  java
  • 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 + "";
            }
        }
    }
    



  • 相关阅读:
    VS中的 MD/MT设置 【转】
    VS2010/MFC编程入门之五十四(Ribbon界面开发:使用更多控件并为控件添加消息处理函数)
    VS2010/MFC编程入门之五十三(Ribbon界面开发:为Ribbon Bar添加控件)[转]
    [MFC]选择目录对话框和选择文件对话框 [转]
    NMM3DViewer 设计
    将可执行程序的内存空间扩展到3GB(windows)
    centos7 安装rocketmq(quick start)
    Centos7 安装 Maven 3.5.*
    ss命令
    强制重启Linux系统的几种方法
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6848196.html
Copyright © 2011-2022 走看看