# DFS

### Maximize difference between pair of nodes in a given rooted tree such that one node is ancestor of another

#include using namespace std;  vector tree;vector visited;vector ancestorNum;  int maxDiff = INT_MIN;  void dfs(int src, int val, vector& W){          visited[src] = true;          for (auto neighbour…

### Minimize sum of node values by filling given empty Tree such that each node is GCD of its children

#include using namespace std;  class Node {public:    int val;    Node *left, *right;    Node(int val)    {        this->val = val;        left = NULL;        right = NULL;    }};  class Tree {public:    unordered_map depth;              int findDepth(Node*…

### Sum of all nodes with smaller values at a distance K from a given node in a BST

#include using namespace std;  struct TreeNode {      int data;    TreeNode* left;    TreeNode* right;          TreeNode(int data)    {        this->data = data;        this->left = NULL;        this->right = NULL;    }};  void kDistanceDownSum(TreeNode* root,                      int k, int&…

### Print all the paths from root to leaf, with a specified sum in Binary tree

import java.util.*;  class GFG {      static List result        = new ArrayList();          static class Node {        int data;        Node left, right;    };          static Node newNode(int data)    {        Node temp = new…

### Find all the Mother Vertices of a graph

#include using namespace std;  void dfs_helper(int u,                vector& adj,                bool visited[]){    if (visited[u])        return;      visited[u] = true;      for (auto v : adj[u]) {        if (!visited[v])            dfs_helper(v, adj, visited);    }}  void getTransposeGraph(    vector&…

### Traverse graph in lexicographical order of nodes using DFS

#include using namespace std;  void LexiDFS(map& G,             char S, map& vis){        vis[S] = true;          cout

### Kth largest element in an N-array Tree

#include using namespace std;  class Node {public:    int data;    vector childs;};  int largestELe = INT_MIN;  void largestEleUnderRange(    Node* root, int data){            if (root->data < data) {        largestELe =…

### Minimum time required to color all edges of a Tree

#include using namespace std;  int ans = 0;  vector edges[100000];  void Add_edge(int u, int v){    edges[u].push_back(v);    edges[v].push_back(u);}  void minTimeToColor(int node, int parent,                    int arrival_time){            int current_time = 0;      for…

### Find the Dominators for every vertex in a given DAG (Directed Acyclic Graph)

#include using namespace std;  vector b(100);  int vis[100] = {};  void findDominator(vector graph,                   bitset par, int node){        if (vis[node] == 0) {                b[node] = ~b[node];                  vis[node] =…

### Lowest Common Ancestor of the deepest leaves of a Binary Tree

#include using namespace std;  struct Node {    struct Node* left;    struct Node* right;    int data;};  Node* newNode(int key){    Node* temp = new Node;    temp->data = key;    temp->left =…