#include <bits/stdc++.h>
int in() {
  char c = getchar();
  int r = 0;
  for (; c < '0' || c > '9'; c = getchar())
    ;
  for (; c >= '0' && c <= '9'; r = r * 10 + c - '0', c = getchar())
    ;
  return r;
}
int n;
std::vector<std::vector<int> > g;
std::vector<int> ans;
std::vector<int> mx;
std::vector<std::vector<int> *> levels;
void dfs(int v, int parent = -1) {
  int biggest = -1;
  for (auto &ch : g[v]) {
    if (ch == parent) continue;
    dfs(ch, v);
    if (biggest == -1 || levels[ch]->size() > levels[biggest]->size())
      biggest = ch;
  }
  if (biggest == -1) {
    ans[v] = 0;
    mx[v] = 1;
    levels[v] = new std::vector<int>();
  } else {
    levels[v] = levels[biggest];
    levels[v]->push_back(1);
    mx[v] = mx[biggest];
    ans[v] = ans[biggest];
    if (ans[biggest] != 0) ans[v]++;
    for (auto &ch : g[v]) {
      if (ch == parent || ch == biggest) continue;
      levels[v]->back()++;
      if (levels[v]->back() >= mx[v]) {
        mx[v] = levels[v]->back();
        ans[v] = 1;
      }
      for (int i = levels[ch]->size() - 1; i >= 0; --i) {
        int ind = levels[v]->size() - (levels[ch]->size() - i) - 1;
        (*levels[v])[ind] += (*levels[ch])[i];
        if ((*levels[v])[ind] > mx[v] ||
            ((*levels[v])[ind] == mx[v] && levels[v]->size() - ind <= ans[v])) {
          ans[v] = levels[v]->size() - ind;
          mx[v] = (*levels[v])[ind];
        }
      }
      delete levels[ch];
    }
  }
}
int main() {
  n = in();
  g.resize(n);
  ans.resize(n);
  mx.resize(n);
  levels.resize(n, nullptr);
  for (int i = 0; i < n - 1; ++i) {
    int a = in(), b = in();
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(0);
  for (auto &i : ans) std::printf("%d\n", i);
  return 0;
}
