#include <bits/stdc++.h>
#pragma GCC optimize(3)
namespace io {
typedef const int& ci;
const size_t _I_Buffer_Size = 1 << 23, _O_Buffer_Size = 1 << 23;
char _I_Buffer[_I_Buffer_Size], *_I_pos = _I_Buffer - 1;
const char* _I_end = _I_Buffer + _I_Buffer_Size;
char _O_Buffer[_O_Buffer_Size], *_O_pos = _O_Buffer - 1;
const char *_O_end = _O_Buffer + _O_Buffer_Size, *_O_preend = _O_end - 1;
struct io {
  __inline__ __attribute__((always_inline)) ~io() {
    fwrite(_O_Buffer, 1, _O_pos - _O_Buffer + 1, stdout);
  }
} ios;
__inline__ __attribute__((always_inline)) char getchar() {
  return *((++_I_pos == _I_end) ? fread(_I_Buffer, 1, _I_Buffer_Size, stdin),
           _I_pos = _I_Buffer   : _I_pos);
}
__inline__ __attribute__((always_inline)) void putchar(ci x) {
  *((_O_pos == _O_preend) ? fwrite(_O_Buffer, 1, _O_end - _O_Buffer, stdout),
    _O_pos = _O_Buffer    : ++_O_pos) = x;
}
template <typename T1>
__inline__ __attribute__((always_inline)) void readuint(T1& x) {
  int ch;
  while ((ch = getchar() ^ '0') > 9)
    ;
  for (x = ch; (ch = getchar() ^ '0') <= 9; x = x * 10 + ch)
    ;
}
template <typename T1, typename T2>
__inline__ __attribute__((always_inline)) void readuint(T1& x, T2& y) {
  readuint(x);
  readuint(y);
}
template <typename T>
void writeuint(const T& x) {
  if (x > 9) writeuint(x / 10);
  putchar((x % 10) | '0');
}
template <typename T>
__inline__ __attribute__((always_inline)) void writelnuint(const T& x) {
  writeuint(x);
  putchar('\n');
}
}  // namespace io
using namespace std;
using io::readuint;
using io::writelnuint;
typedef const int& ci;
int ans[1000000 + 1];
int hsn[1000000 + 1], dp[1000000 + 1], *top = dp;
vector<int> sn[1000000 + 1];
int dfs(ci u, ci p) {
  int hsu, hln = 0, sln;
  for (vector<int>::iterator i = sn[u].begin(), ed = sn[u].end(); i != ed;
       ++i) {
    if (*i != p)
      if ((sln = dfs(*i, u)) > hln) hln = sln, hsu = *i;
  }
  if (hln) {
    hsn[u] = hsu;
    return hln + 1;
  }
  hsn[u] = 0;
  return 1;
}
void dfs(ci u) {
  int hsu, hln = 0, sln;
  for (vector<int>::iterator i = sn[u].begin(), ed = sn[u].end(); i != ed; ++i)
    if ((sln = dfs(*i, u)) > hln) hln = sln, hsu = *i;
  hsn[u] = hsu;
}
void solve(ci u, ci p);
int solve_ret(ci u, ci p) {
  int* dpu = ++top;
  *dpu = 1;
  int hsu = hsn[u];
  if (hsu) {
    int asu = solve_ret(hsu, u);
    int *dpe = top, num = asu ? dpu[++asu] : 1;
    for (vector<int>::iterator i = sn[u].begin(), ed = sn[u].end(); i != ed;
         ++i)
      if (*i != p && *i != hsu) {
        solve(*i, u);
        for (int j = top - dpe; j; --j)
          (dpu[j] += dpe[j]) >= num && (num = dpu[j], asu = j);
        top = dpe;
      }
    return ans[u] = asu;
  }
  return 0;
}
void solve(ci u, ci p) {
  int* dpu = ++top;
  *dpu = 1;
  int hsu = hsn[u];
  if (hsu) {
    int asu = solve_ret(hsu, u);
    int *dpe = top, num = asu ? dpu[++asu] : 1;
    for (vector<int>::iterator i = sn[u].begin(), ed = sn[u].end(); i != ed;
         ++i)
      if (*i != p && *i != hsu) {
        solve(*i, u);
        for (int j = top - dpe; j; --j)
          (dpu[j] += dpe[j]) >= num && (num = dpu[j], asu = j);
        top = dpe;
      }
    ans[u] = asu;
  }
}
int main() {
  int n;
  readuint(n);
  if (n == 1) {
    puts("0");
    return 0;
  }
  for (int i = n, u, v; --i; sn[u].push_back(v), sn[v].push_back(u))
    readuint(u, v);
  dfs(1);
  memset(ans, 0, sizeof(ans));
  solve(1, 0);
  for (int *i = ans, *ed = ans + n; ++i <= ed; writelnuint(*i))
    ;
  return 0;
}
