#include <bits/stdc++.h>
using namespace std;
int read() {
  register int x(0), f(1);
  register char ch(getchar());
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
const int N = 1e6 + 10;
int n, cnt, h[N];
struct node {
  int next, to;
} e[N << 1];
void add(int u, int v) { e[++cnt] = (node){h[u], v}, h[u] = cnt; }
int son[N], len[N], ans[N], tmp[N], *f[N], *id = tmp;
void dfs(int u, int fa) {
  for (int i = h[u], v; v = e[i].to, i; i = e[i].next)
    if (v != fa) {
      dfs(v, u);
      if (len[v] > len[son[u]]) son[u] = v;
    }
  len[u] = len[son[u]] + 1;
}
void Dfs(int u, int fa) {
  f[u][0] = 1;
  if (son[u]) f[son[u]] = f[u] + 1, Dfs(son[u], u), ans[u] = ans[son[u]] + 1;
  for (int i = h[u], v; v = e[i].to, i; i = e[i].next)
    if (v != fa && v != son[u]) {
      f[v] = id;
      id += len[v];
      Dfs(v, u);
      for (int j = 1; j <= len[v]; ++j) {
        f[u][j] += f[v][j - 1];
        if ((j < ans[u] && f[u][j] >= f[u][ans[u]]) ||
            (j > ans[u] && f[u][j] > f[u][ans[u]]))
          ans[u] = j;
      }
    }
  if (f[u][ans[u]] == 1) ans[u] = 0;
}
int main() {
  n = read();
  for (int i = 1; i < n; ++i) {
    int u = read(), v = read();
    add(u, v), add(v, u);
  }
  dfs(1, 0);
  f[1] = id;
  id += len[1];
  Dfs(1, 0);
  for (int i = 1; i <= n; ++i) cout << ans[i] << '\n';
  return 0;
}
