#include <bits/stdc++.h>
using namespace std;
int level[1000003], tree[1000003 * 4], sz[1000003], n, u, v, ans[1000003];
bool big[1000003];
vector<int> g[1000003];
void dfs0(int now, int par, int lev) {
  level[now] = lev;
  sz[now] = 1;
  for (int i : g[now]) {
    if (i == par) continue;
    dfs0(i, now, lev + 1);
    sz[now] += sz[i];
  }
}
void upd(int idx, int l, int r, int in, int val) {
  if (l == r) {
    tree[idx] += val;
    return;
  }
  if (in <= (l + r) / 2)
    upd(2 * idx, l, (l + r) / 2, in, val);
  else
    upd(2 * idx + 1, (l + r) / 2 + 1, r, in, val);
  if (tree[idx * 2] >= tree[idx * 2 + 1]) {
    tree[idx] = tree[idx * 2];
  } else {
    tree[idx] = tree[idx * 2 + 1];
  }
}
int cari(int idx, int l, int r) {
  if (l == r) return l;
  if (tree[2 * idx] >= tree[2 * idx + 1]) return cari(2 * idx, l, (l + r) / 2);
  return cari(2 * idx + 1, (l + r) / 2 + 1, r);
}
void add(int now, int par, int x) {
  if (x == -1) {
    upd(1, 1, n, level[now], -1);
  } else {
    upd(1, 1, n, level[now], 1);
  }
  for (int i : g[now]) {
    if (i == par || big[i]) continue;
    add(i, now, x);
  }
}
void dfs(int now, int par, int keep) {
  int mxx = 0;
  for (int i : g[now]) {
    if (i == par) continue;
    if (sz[i] > sz[mxx]) mxx = i;
  }
  for (int i : g[now]) {
    if (i == par || i == mxx) continue;
    dfs(i, now, 0);
  }
  if (mxx) dfs(mxx, now, 1), big[mxx] = true;
  add(now, par, 1);
  ans[now] = cari(1, 1, n);
  if (mxx) big[mxx] = false;
  if (!keep) add(now, par, -1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs0(1, 0, 1);
  dfs(1, 0, 1);
  for (int i = 1; i <= n; i++) {
    printf("%d\n", ans[i] - level[i]);
  }
}
