#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
vector<int> g[maxn];
int n;
int tmp[maxn], *dp[maxn], *id, ans[maxn], len[maxn], son[maxn];
void prework(int u, int fa) {
  len[u] = 0;
  son[u] = 0;
  for (auto it : g[u]) {
    if (it == fa) continue;
    prework(it, u);
    if (son[u] == 0 || len[it] > len[son[u]]) son[u] = it;
  }
  len[u] = len[son[u]] + 1;
}
void dfs(int u, int fa) {
  dp[u][0] = 1;
  ans[u] = 0;
  if (son[u]) {
    dp[son[u]] = dp[u] + 1;
    dfs(son[u], u);
    if (dp[u][ans[son[u]] + 1] > dp[u][ans[u]]) ans[u] = ans[son[u]] + 1;
  }
  for (auto it : g[u]) {
    if (it == fa || it == son[u]) continue;
    dp[it] = id;
    id += len[it];
    dfs(it, u);
    for (int i = 1; i <= len[it]; i++) {
      dp[u][i] += dp[it][i - 1];
      if (dp[u][i] > dp[u][ans[u]]) {
        ans[u] = i;
      } else if (dp[u][i] == dp[u][ans[u]]) {
        ans[u] = min(ans[u], i);
      }
    }
  }
}
int main() {
  scanf("%d", &n);
  id = tmp;
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    g[x].push_back(y);
    g[y].push_back(x);
  }
  prework(1, 0);
  dp[1] = id;
  id += len[1];
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    printf("%d\n", ans[i]);
  }
  return 0;
}
