Focus Problem – try your best to solve this problem before continuing!
Trees are generally treated very differently from general graph problems.
Resources | ||||
---|---|---|---|---|
CPH | traversing tree, diameter | |||
SecondThread |
Some properties/definitions of trees:
General Tree Terminology:
Rooted Tree Terminology:
In this problem we are given the parent of each node of a rooted tree, and we want to compute the subtree size for each node. A subtree is composed of a root node and the subtrees of the root's children. Thus, the size of a subtree is one plus the size of the root's childrens' subtrees.
#include <bits/stdc++.h>using namespace std;const int SZ = 2e5;vector<int> children[SZ];int subtree_size[SZ], depth[SZ];void dfs_size(int node) {subtree_size[node] = 1; // This one represents the root of `node's` subtree
Status | Source | Problem Name | Difficulty | Tags | |
---|---|---|---|---|---|
CF | Easy | Show TagsConnected Components, Diameter, Tree | |||
Silver | Easy | Show TagsConnected Components, Tree | |||
CF | Easy | Show TagsTree | |||
Silver | Easy | Show TagsConnected Components, Tree | |||
Silver | Easy | Show TagsGreedy, Tree | |||
Bronze | Easy | Show TagsTree | |||
CSES | Normal | Show TagsTree | |||
CF | Normal | Show TagsTree | |||
CSES | Normal | Show TagsTree | |||
CSES | Normal | Show TagsTree | |||
Silver | Normal | Show TagsBipartite, Tree | |||
CSES | Hard | Show TagsDFS, Spanning Tree | |||
CF | Hard | Show TagsDFS, Spanning Tree |
How is a preorder traversal found for a binary tree?