#include <bits/stdc++.h>
using namespace std;
namespace fastIO {
bool IOerror = 0;
inline char nc() {
  static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000;
  if (p1 == pend) {
    p1 = buf;
    pend = buf + fread(buf, 1, 100000, stdin);
    if (pend == p1) {
      IOerror = 1;
      return -1;
    }
  }
  return *p1++;
}
inline bool blank(char ch) {
  return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
}
inline long long read() {
  bool sign = 0;
  char ch = nc();
  long long x = 0;
  for (; blank(ch); ch = nc())
    ;
  if (IOerror) return 0;
  if (ch == '-') sign = 1, ch = nc();
  for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';
  if (sign) x = -x;
  return x;
}
};  // namespace fastIO
using namespace fastIO;
vector<long long> e[2000010];
long long dep[2000010], fa[2000010], son[2000010], top[2000010], id[2000010],
    now = 0, f[2000010];
void dfs1(long long x, long long pre) {
  dep[x] = 1;
  for (auto i : e[x]) {
    if (i == pre) continue;
    dfs1(i, x);
    fa[i] = x;
    if (dep[i] > dep[son[x]]) son[x] = i;
    dep[x] = max(dep[x], dep[i] + 1);
  }
}
void dfs2(long long x, long long tp) {
  id[x] = ++now;
  top[x] = tp;
  if (son[x]) dfs2(son[x], son[x]);
  for (auto i : e[x]) {
    if (i == son[x] || i == fa[x]) continue;
    dfs2(i, tp);
  }
}
long long maxx[1000010], maxi[1000010];
void merge(long long p, long long x, long long y) {
  for (long long i = 1; i <= dep[y]; i++) {
    f[id[x] + i - 1] += f[id[y] + i - 1];
    if (f[id[x] + i - 1] > maxx[p] ||
        (f[id[x] + i - 1] == maxx[p] && i < maxi[p]))
      maxx[p] = f[id[x] + i - 1], maxi[p] = i;
  }
}
void dfs3(long long x, long long pre) {
  if (son[x]) dfs3(son[x], x);
  if (son[x])
    maxx[x] = maxx[son[x]], maxi[x] = maxi[son[x]] + 1;
  else
    maxx[x] = 1, maxi[x] = 0;
  for (auto i : e[x]) {
    if (i == fa[x] || i == son[x]) continue;
    dfs3(i, x);
    merge(x, son[x], i);
  }
  f[id[x]] = 1;
  if (maxx[x] == 1) maxi[x] = 0;
}
signed main() {
  long long n = read();
  for (long long i = 1; i < n; i++) {
    long long x = read(), y = read();
    e[x].push_back(y);
    e[y].push_back(x);
  }
  dfs1(1, 1);
  dfs2(1, 1);
  dfs3(1, 1);
  for (long long i = 1; i <= n; i++) cout << maxi[i] << '\n';
  return 0;
}
