# Tree

### 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*…

### Minimum distance between two given nodes in an N-ary tree

#include using namespace std;  struct Node {    int val;    vector child;};  Node* newNode(int key){    Node* temp = new Node;    temp->val = key;    return temp;}  bool flag;  void findPath(Node* root,…

### Rabbit House | Google Kickstart 2021 Round A

#include using namespace std;  void solve(long long int r, long long int c,           vector& grid){    priority_queue        pq;      for (long long int i = 0; i…

### Height of Factor Tree for a given number

Given a positive integer N, the task is to find the height of the Factor Tree of the given integer…

### Minimize absolute difference between sum of subtrees formed after splitting Binary tree into two

import java.lang.Math;import java.io.*;  class GFG {      static class Node {        Node left, right;        int val;        public Node(int val)        {            this.val = val;            this.left = this.right = null;        }    }                  public static…

### Minimum value to be added at each level in Binary Tree to make all level sum equal

import java.util.*;  class Node {    int data;    Node left, right;    public Node(int item)    {        data = item;        left = right = null;    }}  class BinaryTree {    Node root;    public BinaryTree()    {        root =…

### 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&…