Posts

Showing posts with the label Tree and Graph

Check is Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric:     1    / \   2   2  / \ / \ 3  4 4  3 But the following is not:     1    / \   2   2    \   \    3    3 /**  * Definition for binary tree  * struct TreeNode {  *     int val;  *     TreeNode *left;  *     TreeNode *right;  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}  * };  */ class Solution { public :        bool SymmetricUtil(TreeNode * n1 , TreeNode * n2 ) {               if ( n1 == nullptr && n2 == nullpt...

Graph extended - adjacency list representation

/*** Graph implement: 1. BFS 2. DFS 3. test is cyclic 4. topological sort http://www.geeksforgeeks.org/topological-sorting / http://www.cs.washington.edu/education/courses/cse373/02au/lectures/lecture19l.pdf 5. Find a path between two node in a graph --> if node v is reachable with node w http://www.geeksforgeeks.org/find-if-there-is-a-path-between-two-vertices-in-a-given-graph/ ***/ #include <iostream> #include <list> #include <queue> using namespace std; class graph {        int V;        list < int > *adj;        bool cyclicUtil( int , bool *, bool *);        void BFSUtil( bool *, queue < int > &);        void DFSUtil( int , bool *);        void update_degree( int , int *, queue < int > &);  ...