//I_l_M_P
#include <bits/stdc++.h>

#ifdef FAST_ALLOCATOR_MEMORY
int allocator_pos = 0;
char allocator_memory[(int)FAST_ALLOCATOR_MEMORY];
inline void * operator new ( size_t n ) {
  char *res = allocator_memory + allocator_pos;
  allocator_pos += n;
  assert(allocator_pos <= (int)FAST_ALLOCATOR_MEMORY);
  return (void *)res;
}
inline void operator delete ( void * ) noexcept { }
//inline void * operator new [] ( size_t ) { assert(0); }
//inline void operator delete [] ( void * ) { assert(0); }
#endif

template <class T = int> inline T readInt();
inline double readDouble();
inline int readUInt();
inline int readChar();
inline void readWord(char *s);
inline bool readLine(char *s);
inline bool isEof();
inline int getChar();
inline int peekChar();
inline bool seekEof();
inline void skipBlanks();

template <class T> inline void writeInt(T x, char end = 0, int len = -1);
inline void writeChar(int x);
inline void writeWord(const char *s);
inline void writeDouble(double x, int len = 0);
inline void flush();

static struct buffer_flusher_t {
  ~buffer_flusher_t() {
    flush();
  }
} buffer_flusher;

static const int buf_size = 4096;

static unsigned char buf[buf_size];
static int buf_len = 0, buf_pos = 0;

inline bool isEof() {
  if (buf_pos == buf_len) {
    buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
    if (buf_pos == buf_len)
      return 1;
  }
  return 0;
}

inline int getChar() {
  return isEof() ? -1 : buf[buf_pos++];
}

inline int peekChar() {
  return isEof() ? -1 : buf[buf_pos];
}

inline bool seekEof() {
  int c;
  while ((c = peekChar()) != -1 && c <= 32)
    buf_pos++;
  return c == -1;
}

inline void skipBlanks() {
  while (!isEof() && buf[buf_pos] <= 32U)
    buf_pos++;
}

inline int readChar() {
  int c = getChar();
  while (c != -1 && c <= 32)
    c = getChar();
  return c;
}

inline int readUInt() {
  int c = readChar(), x = 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return x;
}

template <class T>
inline T readInt() {
  int s = 1, c = readChar();
  T x = 0;
  if (c == '-')
    s = -1, c = getChar();
  else if (c == '+')
    c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return s == 1 ? x : -x;
}

inline double readDouble() {
  int s = 1, c = readChar();
  double x = 0;
  if (c == '-')
    s = -1, c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  if (c == '.') {
    c = getChar();
    double coef = 1;
    while ('0' <= c && c <= '9')
      x += (c - '0') * (coef *= 1e-1), c = getChar();
  }
  return s == 1 ? x : -x;
}

inline void readWord(char *s) {
  int c = readChar();
  while (c > 32)
    *s++ = c, c = getChar();
  *s = 0;
}

inline bool readLine(char *s) {
  int c = getChar();
  while (c != '\n' && c != -1)
    *s++ = c, c = getChar();
  *s = 0;
  return c != -1;
}

static int write_buf_pos = 0;
static char write_buf[buf_size];

inline void writeChar( int x ) {
  if (write_buf_pos == buf_size)
    fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  write_buf[write_buf_pos++] = x;
}

inline void flush() {
  if (write_buf_pos) {
    fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
    fflush(stdout);
  }
}

template <class T>
inline void writeInt(T x, char end, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  char s[24];
  int n = 0;
  while (x || !n)
    s[n++] = '0' + x % 10, x /= 10;
  while (n < output_len)
    s[n++] = '0';
  while (n--)
    writeChar(s[n]);
  if (end)
    writeChar(end);
}

inline void writeWord(const char *s) {
  while (*s)
    writeChar(*s++);
}

inline void writeDouble(double x, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  int t = (int)x;
  writeInt(t), x -= t;
  writeChar('.');
  for (int i = output_len - 1; i > 0; i--) {
    x *= 10;
    t = std::min(9, (int)x);
    writeChar('0' + t), x -= t;
  }
  x *= 10;
  t = std::min(9, (int)(x + 0.5));
  writeChar('0' + t);
}

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

//#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

using namespace std;

typedef long double ld;
typedef long long ll;

#define _ inline
#define f first
#define sec second
#define pb push_back
#define ins insert
#define ers erase
#define mk make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
//#define int ll

#define right rightt
#define left leftt
#define prev prevv
#define next nextt

mt19937 Random(time(0));

const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;
const int P = 239017;
const long long BINF = 1e18 + 10;

const int N = 1e6 + 10;
const int SZ = 2e3;

struct query {
  int l, r, num;
  query(int _l, int _r, int _num) : l(_l), r(_r), num(_num) {};
};

bool cmp(query a, query b) {
  return a.l / SZ < b.l / SZ || (a.l / SZ == b.l / SZ && a.r < b.r);
}

vector<int> g[N], order;
int sz[N], h[N], cnt[N], ans[N];
pair<int, int> best = {0, -INF};
vector<int> rlb;
vector<pair<int, int> > rlb_best;

void dfs(int v, int parent = 0, int cur_h = 0) {
  order.pb(v);
  h[v] = cur_h;
  sz[v] = 1;
  for (auto u : g[v]) {
    if (u != parent) {
      dfs(u, v, cur_h + 1);
      sz[v] += sz[u];
    }
  }
}

void add(int ind) {
  int c = h[order[ind]];
  cnt[c]++;
  rlb_best.pb(best);
  rlb.pb(c);
  best = max(best, {cnt[c], -c});
}

void get_rollback() {
  cnt[rlb.back()]--;
  rlb.pop_back();
  best = rlb_best.back();
  rlb_best.pop_back();
}

void source() {
  int n = readInt();
  for (int i = 1; i < n; i++) {
    int u = readInt(), v = readInt();
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  vector<query> q;
  for (int i = 0; i < (int)order.size(); i++) {
    q.emplace_back(i, i + sz[order[i]] - 1, order[i]);
  }
  sort(all(q), cmp);
  pair<int, int> cur;
  for (int i = 0; i < n; i++) {
    if (!i || q[i].l / SZ != q[i - 1].l / SZ) {
      while (!rlb.empty()) {
        get_rollback();
      }
      for (int j = q[i].l; j <= q[i].r; j++) {
        if (j / SZ != q[i].l / SZ) {
          add(j);
        }
      }
      cur = {q[i].l, q[i].r};
    }
    else {
      while (cur.sec < q[i].r) {
        cur.sec++;
        if (cur.sec / SZ != q[i].l / SZ) add(cur.sec);
      }
      cur.f = q[i].l;
    }
    int kek = 0;
    for (int j = q[i].l; j <= q[i].r; j++) {
      if (j / SZ != q[i].l / SZ) {
        break;
      }
      kek++;
      add(j);
    }
    ans[q[i].num] = -best.sec;
    for (int j = 0; j < kek; j++) {
      get_rollback();
    }
  }
  for (int i = 1; i <= n; i++) {
    writeInt(ans[i] - h[i], '\n');
  }
}

signed main() {
#ifdef extraterrestrial_
  assert(freopen("input.txt", "r", stdin));
  //assert(freopen("output.txt", "w", stdout));
#else
#define TASK ""
  //assert(freopen(TASK".in", "r", stdin));
  //assert(freopen(TASK".out", "w", stdout));
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  source();
#ifdef extraterrestrial_
  writeWord("\n\t");
  writeDouble((double)clock() / (double)CLOCKS_PER_SEC, 10);
  writeWord(" seconds\n\n");
#endif
  return 0;
}


//I_l_M_P
#include <bits/stdc++.h>

#ifdef FAST_ALLOCATOR_MEMORY
int allocator_pos = 0;
char allocator_memory[(int)FAST_ALLOCATOR_MEMORY];
inline void * operator new ( size_t n ) {
  char *res = allocator_memory + allocator_pos;
  allocator_pos += n;
  assert(allocator_pos <= (int)FAST_ALLOCATOR_MEMORY);
  return (void *)res;
}
inline void operator delete ( void * ) noexcept { }
//inline void * operator new [] ( size_t ) { assert(0); }
//inline void operator delete [] ( void * ) { assert(0); }
#endif

template <class T = int> inline T readInt();
inline double readDouble();
inline int readUInt();
inline int readChar();
inline void readWord(char *s);
inline bool readLine(char *s);
inline bool isEof();
inline int getChar();
inline int peekChar();
inline bool seekEof();
inline void skipBlanks();

template <class T> inline void writeInt(T x, char end = 0, int len = -1);
inline void writeChar(int x);
inline void writeWord(const char *s);
inline void writeDouble(double x, int len = 0);
inline void flush();

static struct buffer_flusher_t {
  ~buffer_flusher_t() {
    flush();
  }
} buffer_flusher;

static const int buf_size = 4096;

static unsigned char buf[buf_size];
static int buf_len = 0, buf_pos = 0;

inline bool isEof() {
  if (buf_pos == buf_len) {
    buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
    if (buf_pos == buf_len)
      return 1;
  }
  return 0;
}

inline int getChar() {
  return isEof() ? -1 : buf[buf_pos++];
}

inline int peekChar() {
  return isEof() ? -1 : buf[buf_pos];
}

inline bool seekEof() {
  int c;
  while ((c = peekChar()) != -1 && c <= 32)
    buf_pos++;
  return c == -1;
}

inline void skipBlanks() {
  while (!isEof() && buf[buf_pos] <= 32U)
    buf_pos++;
}

inline int readChar() {
  int c = getChar();
  while (c != -1 && c <= 32)
    c = getChar();
  return c;
}

inline int readUInt() {
  int c = readChar(), x = 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return x;
}

template <class T>
inline T readInt() {
  int s = 1, c = readChar();
  T x = 0;
  if (c == '-')
    s = -1, c = getChar();
  else if (c == '+')
    c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return s == 1 ? x : -x;
}

inline double readDouble() {
  int s = 1, c = readChar();
  double x = 0;
  if (c == '-')
    s = -1, c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  if (c == '.') {
    c = getChar();
    double coef = 1;
    while ('0' <= c && c <= '9')
      x += (c - '0') * (coef *= 1e-1), c = getChar();
  }
  return s == 1 ? x : -x;
}

inline void readWord(char *s) {
  int c = readChar();
  while (c > 32)
    *s++ = c, c = getChar();
  *s = 0;
}

inline bool readLine(char *s) {
  int c = getChar();
  while (c != '\n' && c != -1)
    *s++ = c, c = getChar();
  *s = 0;
  return c != -1;
}

static int write_buf_pos = 0;
static char write_buf[buf_size];

inline void writeChar( int x ) {
  if (write_buf_pos == buf_size)
    fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  write_buf[write_buf_pos++] = x;
}

inline void flush() {
  if (write_buf_pos) {
    fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
    fflush(stdout);
  }
}

template <class T>
inline void writeInt(T x, char end, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  char s[24];
  int n = 0;
  while (x || !n)
    s[n++] = '0' + x % 10, x /= 10;
  while (n < output_len)
    s[n++] = '0';
  while (n--)
    writeChar(s[n]);
  if (end)
    writeChar(end);
}

inline void writeWord(const char *s) {
  while (*s)
    writeChar(*s++);
}

inline void writeDouble(double x, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  int t = (int)x;
  writeInt(t), x -= t;
  writeChar('.');
  for (int i = output_len - 1; i > 0; i--) {
    x *= 10;
    t = std::min(9, (int)x);
    writeChar('0' + t), x -= t;
  }
  x *= 10;
  t = std::min(9, (int)(x + 0.5));
  writeChar('0' + t);
}

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

using namespace std;

typedef long double ld;
typedef long long ll;

#define _ inline
#define f first
#define sec second
#define pb push_back
#define ins insert
#define ers erase
#define mk make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
//#define int ll

#define right rightt
#define left leftt
#define prev prevv
#define next nextt

mt19937 Random(time(0));

const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;
const int P = 239017;
const long long BINF = 1e18 + 10;

const int N = 1e6 + 10;
const int SZ = 1e3;

struct query {
  int l, r, num;
  query(int _l, int _r, int _num) : l(_l), r(_r), num(_num) {};
};

bool cmp(query a, query b) {
  return a.l / SZ < b.l / SZ || (a.l / SZ == b.l / SZ && a.r < b.r);
}

vector<int> g[N], order;
int sz[N], h[N], cnt[N], ans[N], cur_max;

void dfs(int v, int parent = 0, int cur_h = 0) {
  order.pb(v);
  h[v] = cur_h;
  sz[v] = 1;
  for (auto u : g[v]) {
    if (u != parent) {
      dfs(u, v, cur_h + 1);
      sz[v] += sz[u];
    }
  }
}

set<pair<int, int> > have;

void add(int ind) {
  int c = h[order[ind]];
  have.ers({cnt[c], -c});
  cnt[c]++;
  have.ins({cnt[c], -c});
}

void del(int ind) {
  int c = h[order[ind]];
  have.ers({cnt[c], -c});
  cnt[c]--;
  have.ins({cnt[c], -c});
}

void source() {
  int n = readInt();
  for (int i = 1; i < n; i++) {
    int u = readInt(), v = readInt();
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  vector<query> q;
  for (int i = 0; i < (int)order.size(); i++) {
    q.emplace_back(i, i + sz[order[i]] - 1, order[i]);
  }
  sort(all(q), cmp);
  pair<int, int> cur;
  for (int i = 0; i < n; i++) {
    if (!i) {
      for (int j = q[i].l; j <= q[i].r; j++) {
        add(j);
      }
      cur = {q[i].l, q[i].r};
    }
    else {
      while (cur.sec < q[i].r) {
        cur.sec++;
        add(cur.sec);
      }
      while (cur.sec > q[i].r) {
        del(cur.sec);
        cur.sec--;
      }
      while (cur.f < q[i].l) {
        del(cur.f);
        cur.f++;
      }
      while (cur.f > q[i].l) {
        cur.f--;
        add(cur.f);
      }
    }
    ans[q[i].num] = -have.rbegin()->sec;
  }
  for (int i = 1; i <= n; i++) {
    writeInt(ans[i] - h[i], '\n');
  }
}

signed main() {
#ifdef extraterrestrial_
  assert(freopen("input.txt", "r", stdin));
  //assert(freopen("output.txt", "w", stdout));
#else
#define TASK ""
  //assert(freopen(TASK".in", "r", stdin));
  //assert(freopen(TASK".out", "w", stdout));
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  source();
#ifdef extraterrestrial_
  writeWord("\n\t");
  writeDouble((double)clock() / (double)CLOCKS_PER_SEC, 10);
  writeWord(" seconds\n\n");
#endif
  return 0;
}


//I_l_M_P
#include <bits/stdc++.h>

#ifdef FAST_ALLOCATOR_MEMORY
int allocator_pos = 0;
char allocator_memory[(int)FAST_ALLOCATOR_MEMORY];
inline void * operator new ( size_t n ) {
  char *res = allocator_memory + allocator_pos;
  allocator_pos += n;
  assert(allocator_pos <= (int)FAST_ALLOCATOR_MEMORY);
  return (void *)res;
}
inline void operator delete ( void * ) noexcept { }
//inline void * operator new [] ( size_t ) { assert(0); }
//inline void operator delete [] ( void * ) { assert(0); }
#endif

template <class T = int> inline T readInt();
inline double readDouble();
inline int readUInt();
inline int readChar();
inline void readWord(char *s);
inline bool readLine(char *s);
inline bool isEof();
inline int getChar();
inline int peekChar();
inline bool seekEof();
inline void skipBlanks();

template <class T> inline void writeInt(T x, char end = 0, int len = -1);
inline void writeChar(int x);
inline void writeWord(const char *s);
inline void writeDouble(double x, int len = 0);
inline void flush();

static struct buffer_flusher_t {
  ~buffer_flusher_t() {
    flush();
  }
} buffer_flusher;

static const int buf_size = 4096;

static unsigned char buf[buf_size];
static int buf_len = 0, buf_pos = 0;

inline bool isEof() {
  if (buf_pos == buf_len) {
    buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
    if (buf_pos == buf_len)
      return 1;
  }
  return 0;
}

inline int getChar() {
  return isEof() ? -1 : buf[buf_pos++];
}

inline int peekChar() {
  return isEof() ? -1 : buf[buf_pos];
}

inline bool seekEof() {
  int c;
  while ((c = peekChar()) != -1 && c <= 32)
    buf_pos++;
  return c == -1;
}

inline void skipBlanks() {
  while (!isEof() && buf[buf_pos] <= 32U)
    buf_pos++;
}

inline int readChar() {
  int c = getChar();
  while (c != -1 && c <= 32)
    c = getChar();
  return c;
}

inline int readUInt() {
  int c = readChar(), x = 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return x;
}

template <class T>
inline T readInt() {
  int s = 1, c = readChar();
  T x = 0;
  if (c == '-')
    s = -1, c = getChar();
  else if (c == '+')
    c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return s == 1 ? x : -x;
}

inline double readDouble() {
  int s = 1, c = readChar();
  double x = 0;
  if (c == '-')
    s = -1, c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  if (c == '.') {
    c = getChar();
    double coef = 1;
    while ('0' <= c && c <= '9')
      x += (c - '0') * (coef *= 1e-1), c = getChar();
  }
  return s == 1 ? x : -x;
}

inline void readWord(char *s) {
  int c = readChar();
  while (c > 32)
    *s++ = c, c = getChar();
  *s = 0;
}

inline bool readLine(char *s) {
  int c = getChar();
  while (c != '\n' && c != -1)
    *s++ = c, c = getChar();
  *s = 0;
  return c != -1;
}

static int write_buf_pos = 0;
static char write_buf[buf_size];

inline void writeChar( int x ) {
  if (write_buf_pos == buf_size)
    fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  write_buf[write_buf_pos++] = x;
}

inline void flush() {
  if (write_buf_pos) {
    fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
    fflush(stdout);
  }
}

template <class T>
inline void writeInt(T x, char end, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  char s[24];
  int n = 0;
  while (x || !n)
    s[n++] = '0' + x % 10, x /= 10;
  while (n < output_len)
    s[n++] = '0';
  while (n--)
    writeChar(s[n]);
  if (end)
    writeChar(end);
}

inline void writeWord(const char *s) {
  while (*s)
    writeChar(*s++);
}

inline void writeDouble(double x, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  int t = (int)x;
  writeInt(t), x -= t;
  writeChar('.');
  for (int i = output_len - 1; i > 0; i--) {
    x *= 10;
    t = std::min(9, (int)x);
    writeChar('0' + t), x -= t;
  }
  x *= 10;
  t = std::min(9, (int)(x + 0.5));
  writeChar('0' + t);
}

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

using namespace std;

typedef long double ld;
typedef long long ll;

#define _ inline
#define f first
#define sec second
#define pb push_back
#define ins insert
#define ers erase
#define mk make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
//#define int ll

#define right rightt
#define left leftt
#define prev prevv
#define next nextt

mt19937 Random(time(0));

const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;
const int P = 239017;
const long long BINF = 1e18 + 10;

const int N = 1e6 + 10;
const int SZ = 1e3;

struct query {
  int l, r, num;
  query(int _l, int _r, int _num) : l(_l), r(_r), num(_num) {};
};

bool cmp(query a, query b) {
  return a.l / SZ < b.l / SZ || (a.l / SZ == b.l / SZ && a.r < b.r);
}

vector<int> g[N], order;
int sz[N], h[N], cnt[N], ans[N];
pair<int, int> best = {0, -INF};

int rlb[N];
pair<int, int> rlb_best[N];
int r1, r2;

void dfs(int v, int parent = 0, int cur_h = 0) {
  order.pb(v);
  h[v] = cur_h;
  sz[v] = 1;
  for (auto u : g[v]) {
    if (u != parent) {
      dfs(u, v, cur_h + 1);
      sz[v] += sz[u];
    }
  }
}

void add(int ind) {
  int c = h[order[ind]];
  cnt[c]++;
  rlb_best[r2++] = best;
  rlb[r1++] = c;
  best = max(best, {cnt[c], -c});
}

void get_rollback() {
  cnt[rlb[--r1]]--;
  best = rlb_best[--r2];
}

void source() {
  int n = readInt();
  for (int i = 1; i < n; i++) {
    int u = readInt(), v = readInt();
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  vector<query> q;
  for (int i = 0; i < (int)order.size(); i++) {
    q.emplace_back(i, i + sz[order[i]] - 1, order[i]);
  }
  sort(all(q), cmp);
  pair<int, int> cur;
  for (int i = 0; i < n; i++) {
    if (!i || q[i].l / SZ != q[i - 1].l / SZ) {
      while (r1 > 0) {
        get_rollback();
      }
      for (int j = q[i].l; j <= q[i].r; j++) {
        if (j / SZ != q[i].l / SZ) {
          add(j);
        }
      }
      cur = {q[i].l, q[i].r};
    }
    else {
      while (cur.sec < q[i].r) {
        cur.sec++;
        if (cur.sec / SZ != q[i].l / SZ) add(cur.sec);
      }
      cur.f = q[i].l;
    }
    int kek = 0;
    for (int j = q[i].l; j <= q[i].r; j++) {
      if (j / SZ != q[i].l / SZ) {
        break;
      }
      kek++;
      add(j);
    }
    ans[q[i].num] = -best.sec;
    for (int j = 0; j < kek; j++) {
      get_rollback();
    }
  }
  for (int i = 1; i <= n; i++) {
    writeInt(ans[i] - h[i], '\n');
  }
}

signed main() {
#ifdef extraterrestrial_
  assert(freopen("input.txt", "r", stdin));
  //assert(freopen("output.txt", "w", stdout));
#else
#define TASK ""
  //assert(freopen(TASK".in", "r", stdin));
  //assert(freopen(TASK".out", "w", stdout));
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  source();
#ifdef extraterrestrial_
  writeWord("\n\t");
  writeDouble((double)clock() / (double)CLOCKS_PER_SEC, 10);
  writeWord(" seconds\n\n");
#endif
  return 0;
}


//I_l_M_P
#include <bits/stdc++.h>

#ifdef FAST_ALLOCATOR_MEMORY
int allocator_pos = 0;
char allocator_memory[(int)FAST_ALLOCATOR_MEMORY];
inline void * operator new ( size_t n ) {
  char *res = allocator_memory + allocator_pos;
  allocator_pos += n;
  assert(allocator_pos <= (int)FAST_ALLOCATOR_MEMORY);
  return (void *)res;
}
inline void operator delete ( void * ) noexcept { }
//inline void * operator new [] ( size_t ) { assert(0); }
//inline void operator delete [] ( void * ) { assert(0); }
#endif

template <class T = int> inline T readInt();
inline double readDouble();
inline int readUInt();
inline int readChar();
inline void readWord(char *s);
inline bool readLine(char *s);
inline bool isEof();
inline int getChar();
inline int peekChar();
inline bool seekEof();
inline void skipBlanks();

template <class T> inline void writeInt(T x, char end = 0, int len = -1);
inline void writeChar(int x);
inline void writeWord(const char *s);
inline void writeDouble(double x, int len = 0);
inline void flush();

static struct buffer_flusher_t {
  ~buffer_flusher_t() {
    flush();
  }
} buffer_flusher;

static const int buf_size = 4096;

static unsigned char buf[buf_size];
static int buf_len = 0, buf_pos = 0;

inline bool isEof() {
  if (buf_pos == buf_len) {
    buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
    if (buf_pos == buf_len)
      return 1;
  }
  return 0;
}

inline int getChar() {
  return isEof() ? -1 : buf[buf_pos++];
}

inline int peekChar() {
  return isEof() ? -1 : buf[buf_pos];
}

inline bool seekEof() {
  int c;
  while ((c = peekChar()) != -1 && c <= 32)
    buf_pos++;
  return c == -1;
}

inline void skipBlanks() {
  while (!isEof() && buf[buf_pos] <= 32U)
    buf_pos++;
}

inline int readChar() {
  int c = getChar();
  while (c != -1 && c <= 32)
    c = getChar();
  return c;
}

inline int readUInt() {
  int c = readChar(), x = 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return x;
}

template <class T>
inline T readInt() {
  int s = 1, c = readChar();
  T x = 0;
  if (c == '-')
    s = -1, c = getChar();
  else if (c == '+')
    c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return s == 1 ? x : -x;
}

inline double readDouble() {
  int s = 1, c = readChar();
  double x = 0;
  if (c == '-')
    s = -1, c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  if (c == '.') {
    c = getChar();
    double coef = 1;
    while ('0' <= c && c <= '9')
      x += (c - '0') * (coef *= 1e-1), c = getChar();
  }
  return s == 1 ? x : -x;
}

inline void readWord(char *s) {
  int c = readChar();
  while (c > 32)
    *s++ = c, c = getChar();
  *s = 0;
}

inline bool readLine(char *s) {
  int c = getChar();
  while (c != '\n' && c != -1)
    *s++ = c, c = getChar();
  *s = 0;
  return c != -1;
}

static int write_buf_pos = 0;
static char write_buf[buf_size];

inline void writeChar( int x ) {
  if (write_buf_pos == buf_size)
    fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  write_buf[write_buf_pos++] = x;
}

inline void flush() {
  if (write_buf_pos) {
    fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
    fflush(stdout);
  }
}

template <class T>
inline void writeInt(T x, char end, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  char s[24];
  int n = 0;
  while (x || !n)
    s[n++] = '0' + x % 10, x /= 10;
  while (n < output_len)
    s[n++] = '0';
  while (n--)
    writeChar(s[n]);
  if (end)
    writeChar(end);
}

inline void writeWord(const char *s) {
  while (*s)
    writeChar(*s++);
}

inline void writeDouble(double x, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  int t = (int)x;
  writeInt(t), x -= t;
  writeChar('.');
  for (int i = output_len - 1; i > 0; i--) {
    x *= 10;
    t = std::min(9, (int)x);
    writeChar('0' + t), x -= t;
  }
  x *= 10;
  t = std::min(9, (int)(x + 0.5));
  writeChar('0' + t);
}

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

//#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

using namespace std;

typedef long double ld;
typedef long long ll;

#define _ inline
#define f first
#define sec second
#define pb push_back
#define ins insert
#define ers erase
#define mk make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
//#define int ll

#define right rightt
#define left leftt
#define prev prevv
#define next nextt

mt19937 Random(time(0));

const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;
const int P = 239017;
const long long BINF = 1e18 + 10;

const int N = 1e6 + 10;
const int SZ = 2e3;

struct query {
  int l, r, num;
  query(int _l, int _r, int _num) : l(_l), r(_r), num(_num) {};
};

bool cmp(query a, query b) {
  return a.l / SZ < b.l / SZ || (a.l / SZ == b.l / SZ && a.r < b.r);
}

vector<int> g[N], order;
int sz[N], h[N], cnt[N], ans[N], cur_max;

void dfs(int v, int parent = 0, int cur_h = 0) {
  order.pb(v);
  h[v] = cur_h;
  sz[v] = 1;
  for (auto u : g[v]) {
    if (u != parent) {
      dfs(u, v, cur_h + 1);
      sz[v] += sz[u];
    }
  }
}

set<pair<int, int> > have;

void add(int ind) {
  int c = h[order[ind]];
  have.ers({cnt[c], -c});
  cnt[c]++;
  have.ins({cnt[c], -c});
}

void del(int ind) {
  int c = h[order[ind]];
  have.ers({cnt[c], -c});
  cnt[c]--;
  have.ins({cnt[c], -c});
}

void source() {
  int n = readInt();
  for (int i = 1; i < n; i++) {
    int u = readInt(), v = readInt();
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  vector<query> q;
  for (int i = 0; i < (int)order.size(); i++) {
    q.emplace_back(i, i + sz[order[i]] - 1, order[i]);
  }
  sort(all(q), cmp);
  pair<int, int> cur;
  for (int i = 0; i < n; i++) {
    if (!i) {
      for (int j = q[i].l; j <= q[i].r; j++) {
        add(j);
      }
      cur = {q[i].l, q[i].r};
    }
    else {
      while (cur.sec < q[i].r) {
        cur.sec++;
        add(cur.sec);
      }
      while (cur.sec > q[i].r) {
        del(cur.sec);
        cur.sec--;
      }
      while (cur.f < q[i].l) {
        del(cur.f);
        cur.f++;
      }
      while (cur.f > q[i].l) {
        cur.f--;
        add(cur.f);
      }
    }
    ans[q[i].num] = -have.rbegin()->sec;
  }
  for (int i = 1; i <= n; i++) {
    writeInt(ans[i] - h[i], '\n');
  }
}

signed main() {
#ifdef extraterrestrial_
  assert(freopen("input.txt", "r", stdin));
  //assert(freopen("output.txt", "w", stdout));
#else
#define TASK ""
  //assert(freopen(TASK".in", "r", stdin));
  //assert(freopen(TASK".out", "w", stdout));
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  source();
#ifdef extraterrestrial_
  writeWord("\n\t");
  writeDouble((double)clock() / (double)CLOCKS_PER_SEC, 10);
  writeWord(" seconds\n\n");
#endif
  return 0;
}


//I_l_M_P
#include <bits/stdc++.h>

#ifdef FAST_ALLOCATOR_MEMORY
int allocator_pos = 0;
char allocator_memory[(int)FAST_ALLOCATOR_MEMORY];
inline void * operator new ( size_t n ) {
  char *res = allocator_memory + allocator_pos;
  allocator_pos += n;
  assert(allocator_pos <= (int)FAST_ALLOCATOR_MEMORY);
  return (void *)res;
}
inline void operator delete ( void * ) noexcept { }
//inline void * operator new [] ( size_t ) { assert(0); }
//inline void operator delete [] ( void * ) { assert(0); }
#endif

template <class T = int> inline T readInt();
inline double readDouble();
inline int readUInt();
inline int readChar();
inline void readWord(char *s);
inline bool readLine(char *s);
inline bool isEof();
inline int getChar();
inline int peekChar();
inline bool seekEof();
inline void skipBlanks();

template <class T> inline void writeInt(T x, char end = 0, int len = -1);
inline void writeChar(int x);
inline void writeWord(const char *s);
inline void writeDouble(double x, int len = 0);
inline void flush();

static struct buffer_flusher_t {
  ~buffer_flusher_t() {
    flush();
  }
} buffer_flusher;

static const int buf_size = 4096;

static unsigned char buf[buf_size];
static int buf_len = 0, buf_pos = 0;

inline bool isEof() {
  if (buf_pos == buf_len) {
    buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
    if (buf_pos == buf_len)
      return 1;
  }
  return 0;
}

inline int getChar() {
  return isEof() ? -1 : buf[buf_pos++];
}

inline int peekChar() {
  return isEof() ? -1 : buf[buf_pos];
}

inline bool seekEof() {
  int c;
  while ((c = peekChar()) != -1 && c <= 32)
    buf_pos++;
  return c == -1;
}

inline void skipBlanks() {
  while (!isEof() && buf[buf_pos] <= 32U)
    buf_pos++;
}

inline int readChar() {
  int c = getChar();
  while (c != -1 && c <= 32)
    c = getChar();
  return c;
}

inline int readUInt() {
  int c = readChar(), x = 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return x;
}

template <class T>
inline T readInt() {
  int s = 1, c = readChar();
  T x = 0;
  if (c == '-')
    s = -1, c = getChar();
  else if (c == '+')
    c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return s == 1 ? x : -x;
}

inline double readDouble() {
  int s = 1, c = readChar();
  double x = 0;
  if (c == '-')
    s = -1, c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  if (c == '.') {
    c = getChar();
    double coef = 1;
    while ('0' <= c && c <= '9')
      x += (c - '0') * (coef *= 1e-1), c = getChar();
  }
  return s == 1 ? x : -x;
}

inline void readWord(char *s) {
  int c = readChar();
  while (c > 32)
    *s++ = c, c = getChar();
  *s = 0;
}

inline bool readLine(char *s) {
  int c = getChar();
  while (c != '\n' && c != -1)
    *s++ = c, c = getChar();
  *s = 0;
  return c != -1;
}

static int write_buf_pos = 0;
static char write_buf[buf_size];

inline void writeChar( int x ) {
  if (write_buf_pos == buf_size)
    fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  write_buf[write_buf_pos++] = x;
}

inline void flush() {
  if (write_buf_pos) {
    fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
    fflush(stdout);
  }
}

template <class T>
inline void writeInt(T x, char end, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  char s[24];
  int n = 0;
  while (x || !n)
    s[n++] = '0' + x % 10, x /= 10;
  while (n < output_len)
    s[n++] = '0';
  while (n--)
    writeChar(s[n]);
  if (end)
    writeChar(end);
}

inline void writeWord(const char *s) {
  while (*s)
    writeChar(*s++);
}

inline void writeDouble(double x, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  int t = (int)x;
  writeInt(t), x -= t;
  writeChar('.');
  for (int i = output_len - 1; i > 0; i--) {
    x *= 10;
    t = std::min(9, (int)x);
    writeChar('0' + t), x -= t;
  }
  x *= 10;
  t = std::min(9, (int)(x + 0.5));
  writeChar('0' + t);
}

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

using namespace std;

typedef long double ld;
typedef long long ll;

#define _ inline
#define f first
#define sec second
#define pb push_back
#define ins insert
#define ers erase
#define mk make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
//#define int ll

#define right rightt
#define left leftt
#define prev prevv
#define next nextt

mt19937 Random(time(0));

const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;
const int P = 239017;
const long long BINF = 1e18 + 10;

const int N = 1e6 + 10;
const int SZ = 1e3;

struct query {
  int l, r, num;
  query(int _l, int _r, int _num) : l(_l), r(_r), num(_num) {};
};

bool cmp(query a, query b) {
  return a.l / SZ < b.l / SZ || (a.l / SZ == b.l / SZ && a.r < b.r);
}

vector<int> g[N], order;
int sz[N], h[N], cnt[N], ans[N];
pair<int, int> best = {0, -INF};

int rlb[N];
pair<int, int> rlb_best[N];
int r1, r2;

void dfs(int v, int parent = 0, int cur_h = 0) {
  order.pb(v);
  h[v] = cur_h;
  sz[v] = 1;
  for (auto u : g[v]) {
    if (u != parent) {
      dfs(u, v, cur_h + 1);
      sz[v] += sz[u];
    }
  }
}

void add(int ind) {
  int c = h[order[ind]];
  cnt[c]++;
  rlb_best[r2++] = best;
  rlb[r1++] = c;
  best = max(best, {cnt[c], -c});
}

void get_rollback() {
  cnt[rlb[--r1]]--;
  best = rlb_best[--r2];
}

void source() {
  int n = readInt();
  for (int i = 1; i < n; i++) {
    int u = readInt(), v = readInt();
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  bool t = true;
  for (int i = 0; i < n; i++) {
    if (h[i] != 1) {
      t = false;
      break;
    }
  }
  if (t) {
    for (int i = 0; i < n; i++) {
      writeInt(0, '\n');
    }
    exit(0);
  }
  vector<query> q;
  for (int i = 0; i < (int)order.size(); i++) {
    q.emplace_back(i, i + sz[order[i]] - 1, order[i]);
  }
  sort(all(q), cmp);
  pair<int, int> cur;
  for (int i = 0; i < n; i++) {
    if (!i || q[i].l / SZ != q[i - 1].l / SZ) {
      while (r1 > 0) {
        get_rollback();
      }
      for (int j = q[i].l; j <= q[i].r; j++) {
        if (j / SZ != q[i].l / SZ) {
          add(j);
        }
      }
      cur = {q[i].l, q[i].r};
    }
    else {
      while (cur.sec < q[i].r) {
        cur.sec++;
        if (cur.sec / SZ != q[i].l / SZ) add(cur.sec);
      }
      cur.f = q[i].l;
    }
    int kek = 0;
    for (int j = q[i].l; j <= q[i].r; j++) {
      if (j / SZ != q[i].l / SZ) {
        break;
      }
      kek++;
      add(j);
    }
    ans[q[i].num] = -best.sec;
    for (int j = 0; j < kek; j++) {
      get_rollback();
    }
  }
  for (int i = 1; i <= n; i++) {
    writeInt(ans[i] - h[i], '\n');
  }
}

signed main() {
#ifdef extraterrestrial_
  assert(freopen("input.txt", "r", stdin));
  //assert(freopen("output.txt", "w", stdout));
#else
#define TASK ""
  //assert(freopen(TASK".in", "r", stdin));
  //assert(freopen(TASK".out", "w", stdout));
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  source();
#ifdef extraterrestrial_
  writeWord("\n\t");
  writeDouble((double)clock() / (double)CLOCKS_PER_SEC, 10);
  writeWord(" seconds\n\n");
#endif
  return 0;
}


//I_l_M_P
#include <bits/stdc++.h>

#ifdef FAST_ALLOCATOR_MEMORY
int allocator_pos = 0;
char allocator_memory[(int)FAST_ALLOCATOR_MEMORY];
inline void * operator new ( size_t n ) {
  char *res = allocator_memory + allocator_pos;
  allocator_pos += n;
  assert(allocator_pos <= (int)FAST_ALLOCATOR_MEMORY);
  return (void *)res;
}
inline void operator delete ( void * ) noexcept { }
//inline void * operator new [] ( size_t ) { assert(0); }
//inline void operator delete [] ( void * ) { assert(0); }
#endif

template <class T = int> inline T readInt();
inline double readDouble();
inline int readUInt();
inline int readChar();
inline void readWord(char *s);
inline bool readLine(char *s);
inline bool isEof();
inline int getChar();
inline int peekChar();
inline bool seekEof();
inline void skipBlanks();

template <class T> inline void writeInt(T x, char end = 0, int len = -1);
inline void writeChar(int x);
inline void writeWord(const char *s);
inline void writeDouble(double x, int len = 0);
inline void flush();

static struct buffer_flusher_t {
  ~buffer_flusher_t() {
    flush();
  }
} buffer_flusher;

static const int buf_size = 4096;

static unsigned char buf[buf_size];
static int buf_len = 0, buf_pos = 0;

inline bool isEof() {
  if (buf_pos == buf_len) {
    buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
    if (buf_pos == buf_len)
      return 1;
  }
  return 0;
}

inline int getChar() {
  return isEof() ? -1 : buf[buf_pos++];
}

inline int peekChar() {
  return isEof() ? -1 : buf[buf_pos];
}

inline bool seekEof() {
  int c;
  while ((c = peekChar()) != -1 && c <= 32)
    buf_pos++;
  return c == -1;
}

inline void skipBlanks() {
  while (!isEof() && buf[buf_pos] <= 32U)
    buf_pos++;
}

inline int readChar() {
  int c = getChar();
  while (c != -1 && c <= 32)
    c = getChar();
  return c;
}

inline int readUInt() {
  int c = readChar(), x = 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return x;
}

template <class T>
inline T readInt() {
  int s = 1, c = readChar();
  T x = 0;
  if (c == '-')
    s = -1, c = getChar();
  else if (c == '+')
    c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return s == 1 ? x : -x;
}

inline double readDouble() {
  int s = 1, c = readChar();
  double x = 0;
  if (c == '-')
    s = -1, c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  if (c == '.') {
    c = getChar();
    double coef = 1;
    while ('0' <= c && c <= '9')
      x += (c - '0') * (coef *= 1e-1), c = getChar();
  }
  return s == 1 ? x : -x;
}

inline void readWord(char *s) {
  int c = readChar();
  while (c > 32)
    *s++ = c, c = getChar();
  *s = 0;
}

inline bool readLine(char *s) {
  int c = getChar();
  while (c != '\n' && c != -1)
    *s++ = c, c = getChar();
  *s = 0;
  return c != -1;
}

static int write_buf_pos = 0;
static char write_buf[buf_size];

inline void writeChar( int x ) {
  if (write_buf_pos == buf_size)
    fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  write_buf[write_buf_pos++] = x;
}

inline void flush() {
  if (write_buf_pos) {
    fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
    fflush(stdout);
  }
}

template <class T>
inline void writeInt(T x, char end, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  char s[24];
  int n = 0;
  while (x || !n)
    s[n++] = '0' + x % 10, x /= 10;
  while (n < output_len)
    s[n++] = '0';
  while (n--)
    writeChar(s[n]);
  if (end)
    writeChar(end);
}

inline void writeWord(const char *s) {
  while (*s)
    writeChar(*s++);
}

inline void writeDouble(double x, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  int t = (int)x;
  writeInt(t), x -= t;
  writeChar('.');
  for (int i = output_len - 1; i > 0; i--) {
    x *= 10;
    t = std::min(9, (int)x);
    writeChar('0' + t), x -= t;
  }
  x *= 10;
  t = std::min(9, (int)(x + 0.5));
  writeChar('0' + t);
}

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

//#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

using namespace std;

typedef long double ld;
typedef long long ll;

#define _ inline
#define f first
#define sec second
#define pb push_back
#define ins insert
#define ers erase
#define mk make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
//#define int ll

#define right rightt
#define left leftt
#define prev prevv
#define next nextt

mt19937 Random(time(0));

const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;
const int P = 239017;
const long long BINF = 1e18 + 10;

const int N = 1e6 + 10;
const int SZ = 3e3;

struct query {
  int l, r, num;
  query(int _l, int _r, int _num) : l(_l), r(_r), num(_num) {};
};

bool cmp(query a, query b) {
  return a.l / SZ < b.l / SZ || (a.l / SZ == b.l / SZ && a.r < b.r);
}

vector<int> g[N], order;
int sz[N], h[N], cnt[N], ans[N];
pair<int, int> best = {0, -INF};

int rlb[N];
pair<int, int> rlb_best[N];
int r1, r2;

void dfs(int v, int parent = 0, int cur_h = 0) {
  order.pb(v);
  h[v] = cur_h;
  sz[v] = 1;
  for (auto u : g[v]) {
    if (u != parent) {
      dfs(u, v, cur_h + 1);
      sz[v] += sz[u];
    }
  }
}

void add(int ind) {
  int c = h[order[ind]];
  cnt[c]++;
  rlb_best[r2++] = best;
  rlb[r1++] = c;
  best = max(best, {cnt[c], -c});
}

void get_rollback() {
  cnt[rlb[--r1]]--;
  best = rlb_best[--r2];
}

void source() {
  int n = readInt();
  for (int i = 1; i < n; i++) {
    int u = readInt(), v = readInt();
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  vector<query> q;
  for (int i = 0; i < (int)order.size(); i++) {
    q.emplace_back(i, i + sz[order[i]] - 1, order[i]);
  }
  sort(all(q), cmp);
  pair<int, int> cur;
  for (int i = 0; i < n; i++) {
    if (!i || q[i].l / SZ != q[i - 1].l / SZ) {
      while (r1 > 0) {
        get_rollback();
      }
      for (int j = q[i].l; j <= q[i].r; j++) {
        if (j / SZ != q[i].l / SZ) {
          add(j);
        }
      }
      cur = {q[i].l, q[i].r};
    }
    else {
      while (cur.sec < q[i].r) {
        cur.sec++;
        if (cur.sec / SZ != q[i].l / SZ) add(cur.sec);
      }
      cur.f = q[i].l;
    }
    int kek = 0;
    for (int j = q[i].l; j <= q[i].r; j++) {
      if (j / SZ != q[i].l / SZ) {
        break;
      }
      kek++;
      add(j);
    }
    ans[q[i].num] = -best.sec;
    for (int j = 0; j < kek; j++) {
      get_rollback();
    }
  }
  for (int i = 1; i <= n; i++) {
    writeInt(ans[i] - h[i], '\n');
  }
}

signed main() {
#ifdef extraterrestrial_
  assert(freopen("input.txt", "r", stdin));
  //assert(freopen("output.txt", "w", stdout));
#else
#define TASK ""
  //assert(freopen(TASK".in", "r", stdin));
  //assert(freopen(TASK".out", "w", stdout));
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  source();
#ifdef extraterrestrial_
  writeWord("\n\t");
  writeDouble((double)clock() / (double)CLOCKS_PER_SEC, 10);
  writeWord(" seconds\n\n");
#endif
  return 0;
}


#include <bits/stdc++.h>
const int N=1001*1001;
using namespace std;
struct T{
    vector<int>*mp;
    int a1=0,a2=0;
    int S(){
        return (*mp).size();
    }
    void upd(int key,int val){
        (*mp)[key]+=val;
        if((*mp)[key]>a1){
            a1=(*mp)[key];
            a2=key;
        }else if((*mp)[key]==a1){
            a2=max(a2,key);
        }
    }
}A[N];
T MERGE(T a,T b){
    if(a.S()<b.S())swap(a,b);
    int as=a.S();
    int bs=b.S();
    for(int i=0;i<bs;i++){
        a.upd(as-i-1,(*(b.mp))[bs-i-1]);
    }
    return a;
}
vector<int>g[N];
int n,ans[N];
void dfs(int u,int pr,int dep=0){
    A[u].mp=new vector<int>;
    for(auto v:g[u]){
        if(v==pr)continue;
        dfs(v,u,dep+1);
        A[u]=MERGE(A[v],A[u]);
    }
    A[u].mp->push_back(1);
    A[u].upd(A[u].S()-1,1);
    ans[u]=A[u].S()-A[u].a2-1;
}
int main()
{ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
    cin>>n;
    for(int i=1;i<n;i++){
        int u,v;cin>>u>>v;g[u].push_back(v);g[v].push_back(u);
    }
    dfs(1,0,0);
    for(int i=1;i<=n;i++)cout<<ans[i]<<'\n';
    return 0;
}


#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
const int N=1001*1001;
using namespace std;
map<int,int>*mp[N];
vector<int>g[N];
int n,ans[N];
int a1[N],a2[N];
inline void upd(int u,int key,int val){
    (*mp[u])[key]+=val;
    if((*mp[u])[key]>a1[u]){
        a1[u]=(*mp[u])[key];
        a2[u]=key;
    }else if((*mp[u])[key]==a1[u]){
        a2[u]=min(a2[u],key);
    }
}
void dfs(int u,int pr,int dep=0){
    int mxsz=-1,mxn=-1;
    for(auto v:g[u]){
        if(v==pr)continue;
        dfs(v,u,dep+1);
        if((*mp[v]).size()>mxsz){
            mxsz=(*mp[v]).size();
            mxn=v;
        }
    }
    if(mxn==-1)mp[u]=new map<int,int>();
    else{
            swap(mp[u],mp[mxn]);
            a1[u]=a1[mxn];
            a2[u]=a2[mxn];
    }
    upd(u,dep,1);
    for(auto v:g[u]){
        if(v==pr)continue;
        if(v==mxn)continue;
        for(auto el:(*mp[v]))upd(u,el.first,el.second);
        delete mp[v];
    }
    ans[u]=a2[u]-dep;
}
int main()
{ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
    cin>>n;
    for(int i=1;i<n;i++){
        int u,v;cin>>u>>v;g[u].push_back(v);g[v].push_back(u);
    }
    dfs(1,0,0);
    for(int i=1;i<=n;i++)cout<<ans[i]<<'\n';
    return 0;
}


#include <bits/stdc++.h>
const int N=1001*1001;
using namespace std;
map<int,int>*mp[N];
vector<int>g[N];
int n,ans[N];
int a1[N],a2[N];
void upd(int u,int key,int val){
    (*mp[u])[key]+=val;
    if((*mp[u])[key]>a1[u]){
        a1[u]=(*mp[u])[key];
        a2[u]=key;
    }else if((*mp[u])[key]==a1[u]){
        a2[u]=min(a2[u],key);
    }
}
void dfs(int u,int pr,int dep=0){
    int mxsz=-1,mxn=-1;
    for(auto v:g[u]){
        if(v==pr)continue;
        dfs(v,u,dep+1);
        if((*mp[v]).size()>mxsz){
            mxsz=(*mp[v]).size();
            mxn=v;
        }
    }
    if(mxn==-1)mp[u]=new map<int,int>();
    else{
            (mp[u])=mp[mxn];
            a1[u]=a1[mxn];
            a2[u]=a2[mxn];
    }
    upd(u,dep,1);
    for(auto v:g[u]){
        if(v==pr)continue;
        if(v==mxn)continue;
        for(auto el:(*mp[v]))upd(u,el.first,el.second);
    }
    ans[u]=a2[u]-dep;
}
int main()
{ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
    cin>>n;
    for(int i=1;i<n;i++){
        int u,v;cin>>u>>v;g[u].push_back(v);g[v].push_back(u);
    }
    dfs(1,0,0);
    for(int i=1;i<=n;i++)cout<<ans[i]<<'\n';
    return 0;
}


#include <bits/stdc++.h>
const int N=1001*1001;
using namespace std;
map<int,int>*mp[N];
vector<int>g[N];
int n,ans[N];
int a1[N],a2[N];
void upd(int u,int key,int val){
    (*mp[u])[key]+=val;
    if((*mp[u])[key]>a1[u]){
        a1[u]=(*mp[u])[key];
        a2[u]=key;
    }else if((*mp[u])[key]==a1[u]){
        a2[u]=min(a2[u],key);
    }
}
void dfs(int u,int pr,int dep=0){
    int mxsz=-1,mxn=-1;
    for(auto v:g[u]){
        if(v==pr)continue;
        dfs(v,u,dep+1);
        if((*mp[v]).size()>mxsz){
            mxsz=(*mp[v]).size();
            mxn=v;
        }
    }
    if(mxn==-1)mp[u]=new map<int,int>();
    else{
            swap(mp[u],mp[mxn]);
            a1[u]=a1[mxn];
            a2[u]=a2[mxn];
    }
    upd(u,dep,1);
    for(auto v:g[u]){
        if(v==pr)continue;
        if(v==mxn)continue;
        for(auto el:(*mp[v]))upd(u,el.first,el.second);
        delete mp[v];
    }
    ans[u]=a2[u]-dep;
}
int main()
{ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
    cin>>n;
    for(int i=1;i<n;i++){
        int u,v;cin>>u>>v;g[u].push_back(v);g[v].push_back(u);
    }
    dfs(1,0,0);
    for(int i=1;i<=n;i++)cout<<ans[i]<<'\n';
    return 0;
}


#include <bits/stdc++.h>
const int N=1001*1001;
using namespace std;
struct T{
    vector<int>*mp;
    int a1=0,a2=0;
    int S(){
        return (*mp).size();
    }
    void upd(int key,int val){
        (*mp)[key]+=val;
        if((*mp)[key]>a1){
            a1=(*mp)[key];
            a2=key;
        }else if((*mp)[key]==a1){
            a2=min(a2,key);
        }
    }
}A[N];
T MERGE(T a,T b){
    if(a.S()<b.S())swap(a,b);
    int as=a.S();
    int bs=b.S();
    for(int i=0;i<bs;i++){
        a.upd(as-i-1,(*(b.mp))[bs-i-1]);
    }
    return a;
}
vector<int>g[N];
int n,ans[N];
void dfs(int u,int pr,int dep=0){
    A[u].mp=new vector<int>;
    for(auto v:g[u]){
        if(v==pr)continue;
        dfs(v,u,dep+1);
        A[u]=MERGE(A[v],A[u]);
    }
    A[u].mp->push_back(1);
    A[u].upd(A[u].S()-1,1);
    ans[u]=A[u].S()-A[u].a2-1;
}
int main()
{ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
    cin>>n;
    for(int i=1;i<n;i++){
        int u,v;cin>>u>>v;g[u].push_back(v);g[v].push_back(u);
    }
    dfs(1,0,0);
    for(int i=1;i<=n;i++)cout<<ans[i]<<'\n';
    return 0;
}


#include <bits/stdc++.h>
const int N=1001*1001;
using namespace std;
map<int,int>*mp[N];
vector<int>g[N];
int n,ans[N];
int a1[N],a2[N];
void upd(int u,int key,int val){
    (*mp[u])[key]+=val;
    if((*mp[u])[key]>a1[u]){
        a1[u]=(*mp[u])[key];
        a2[u]=key;
    }else if((*mp[u])[key]==a1[u]){
        a2[u]=min(a2[u],key);
    }
}
void dfs(int u,int pr,int dep=0){
    int mxsz=-1,mxn=-1;
    for(auto v:g[u]){
        if(v==pr)continue;
        dfs(v,u,dep+1);
        if((*mp[v]).size()>mxsz){
            mxsz=(*mp[v]).size();
            mxn=v;
        }
    }
    if(mxn==-1)mp[u]=new map<int,int>();
    else{
            swap(mp[u],mp[mxn]);
            a1[u]=a1[mxn];
            a2[u]=a2[mxn];
    }
    upd(u,dep,1);
    for(auto v:g[u]){
        if(v==pr)continue;
        if(v==mxn)continue;
        for(auto el:(*mp[v]))upd(u,el.first,el.second);
    }
    ans[u]=a2[u]-dep;
}
int main()
{ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
    cin>>n;
    for(int i=1;i<n;i++){
        int u,v;cin>>u>>v;g[u].push_back(v);g[v].push_back(u);
    }
    dfs(1,0,0);
    for(int i=1;i<=n;i++)cout<<ans[i]<<'\n';
    return 0;
}


#include <bits/stdc++.h>
const int N=1001*1001;
using namespace std;
unordered_map<int,int>*mp[N];
vector<int>g[N];
int n,ans[N];
int a1[N],a2[N];
inline void upd(int u,int key,int val){
    (*mp[u])[key]+=val;
    if((*mp[u])[key]>a1[u]){
        a1[u]=(*mp[u])[key];
        a2[u]=key;
    }else if((*mp[u])[key]==a1[u]){
        a2[u]=min(a2[u],key);
    }
}
void dfs(int u,int pr,int dep=0){
    int mxsz=-1,mxn=-1;
    for(auto v:g[u]){
        if(v==pr)continue;
        dfs(v,u,dep+1);
        if((*mp[v]).size()>mxsz){
            mxsz=(*mp[v]).size();
            mxn=v;
        }
    }
    if(mxn==-1)mp[u]=new unordered_map<int,int>();
    else{
            swap(mp[u],mp[mxn]);
            a1[u]=a1[mxn];
            a2[u]=a2[mxn];
    }
    upd(u,dep,1);
    for(auto v:g[u]){
        if(v==pr)continue;
        if(v==mxn)continue;
        for(auto el:(*mp[v]))upd(u,el.first,el.second);
        delete mp[v];
    }
    ans[u]=a2[u]-dep;
}
int main()
{ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
    cin>>n;
    for(int i=1;i<n;i++){
        int u,v;cin>>u>>v;g[u].push_back(v);g[v].push_back(u);
    }
    dfs(1,0,0);
    for(int i=1;i<=n;i++)cout<<ans[i]<<'\n';
    return 0;
}


#include <bits/stdc++.h>
const int N=1001*1001;
using namespace std;
unordered_map<int,int>*mp[N];
vector<int>g[N];
int n,ans[N];
int a1[N],a2[N];
inline void upd(int u,int key,int val){
    (*mp[u])[key]+=val;
    if((*mp[u])[key]>a1[u]){
        a1[u]=(*mp[u])[key];
        a2[u]=key;
    }else if((*mp[u])[key]==a1[u]){
        a2[u]=min(a2[u],key);
    }
}
void dfs(int u,int pr,int dep=0){
    int mxsz=-1,mxn=-1;
    for(auto v:g[u]){
        if(v==pr)continue;
        dfs(v,u,dep+1);
        if((*mp[v]).size()>mxsz){
            mxsz=(*mp[v]).size();
            mxn=v;
        }
    }
    if(mxn==-1)mp[u]=new map<int,int>();
    else{
            swap(mp[u],mp[mxn]);
            a1[u]=a1[mxn];
            a2[u]=a2[mxn];
    }
    upd(u,dep,1);
    for(auto v:g[u]){
        if(v==pr)continue;
        if(v==mxn)continue;
        for(auto el:(*mp[v]))upd(u,el.first,el.second);
        delete mp[v];
    }
    ans[u]=a2[u]-dep;
}
int main()
{ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
    cin>>n;
    for(int i=1;i<n;i++){
        int u,v;cin>>u>>v;g[u].push_back(v);g[v].push_back(u);
    }
    dfs(1,0,0);
    for(int i=1;i<=n;i++)cout<<ans[i]<<'\n';
    return 0;
}


#include <bits/stdc++.h>
const int N=1001*1001;
using namespace std;
map<int,int>*mp[N];
vector<int>g[N];
int n,ans[N];
int a1[N],a2[N];
void dfs(int u,int pr,int dep=0){
    int mxsz=-1,mxn=-1;
    for(auto v:g[u]){
        if(v==pr)continue;
        dfs(v,u,dep+1);
        if((*mp[v]).size()>mxsz){
            mxsz=(*mp[v]).size();
            mxn=v;
        }
    }
    if(mxn==-1)mp[u]=new map<int,int>();
    else{
            swap(mp[u],mp[mxn]);
            a1[u]=a1[mxn];
            a2[u]=a2[mxn];
    }
    {
        (*mp[u])[dep]+=1;
        if((*mp[u])[dep]>a1[u]){
            a1[u]=(*mp[u])[dep];
            a2[u]=dep;
        }else if((*mp[u])[dep]==a1[u]){
            a2[u]=min(a2[u],dep);
        }
    }
    for(auto v:g[u]){
        if(v==pr)continue;
        if(v==mxn)continue;
        for(auto el:(*mp[v])){
            (*mp[u])[el.first]+=el.second;
            if((*mp[u])[el.first]>a1[u]){
                a1[u]=(*mp[u])[el.first];
                a2[u]=el.first;
            }else if((*mp[u])[el.first]==a1[u]){
                a2[u]=min(a2[u],el.first);
            }
        }
        delete mp[v];
    }
    ans[u]=a2[u]-dep;
}
int main()
{ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
    cin>>n;
    for(int i=1;i<n;i++){
        int u,v;cin>>u>>v;g[u].push_back(v);g[v].push_back(u);
    }
    dfs(1,0,0);
    for(int i=1;i<=n;i++)cout<<ans[i]<<'\n';
    return 0;
}


#include <bits/stdc++.h>
#define ls x<<1
#define rs x<<1|1
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
	bool IOerror=0;
	inline char nc() {
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend) {
			p1=buf;
			pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1) {
				IOerror=1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
	}
	template<class T> inline bool read(T &x) {
		bool sign=0;
		char ch=nc();
		x=0;
		for(; blank(ch); ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t) {
		return read(h)&&read(t...);
	}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1e6+10;

vector<int>e[N];
int siz[N],dep[N],son[N],ans[N];
map<int,int>sum;

void dfs1(int u,int fa){
	dep[u]=dep[fa]+1;
	siz[u]=1;
	for(auto v:e[u]){
		if(v==fa) continue;
		dfs1(v,u);
		siz[u]+=siz[v];
		if(siz[v]>siz[son[u]]) son[u]=v;
	}
}

void add(int u,int fa){
	sum[dep[u]]++;
	for(auto v:e[u]) if(v!=fa) add(v,u);
}

void del(int u,int fa){
	sum[dep[u]]--;
	if(sum[dep[u]]==0) sum.erase(sum.find(dep[u]));
	for(auto v:e[u]) if(v!=fa) del(v,u);
}

void dfs2(int u,int fa){
	for(auto v:e[u]){
		if(v==fa || v==son[u]) continue;
		dfs2(v,u);
		del(v,u);
	}
	if(son[u]) dfs2(son[u],u);
	for(auto v:e[u]){
		if(v==son[u] || v==fa) continue;
		add(v,u);
	}
	sum[dep[u]]++;
	int ma=-1,res=0;
	for(auto x:sum){
		if(x.se>ma){
			res=x.fi;
			ma=x.se;
		}
	}
	ans[u]=res-dep[u];
}

int main() {

#ifdef xiaofan
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
#endif

	int n;
	read(n);
	for(int i=1;i<n;i++){
		int u,v;
		read(u,v);
		e[u].pb(v);
		e[v].pb(u);
	}
	dfs1(1,0);
	dfs2(1,0);
	for(int i=1;i<=n;i++) printf("%d\n",ans[i]);

	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
	bool IOerror=0;
	inline char nc() {
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend) {
			p1=buf;
			pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1) {
				IOerror=1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
	}
	template<class T> inline bool read(T &x) {
		bool sign=0;
		char ch=nc();
		x=0;
		for(; blank(ch); ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t) {
		return read(h)&&read(t...);
	}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1e6+1;

struct node {
	int ls,rs,ma,id;
} tree[N * 32];

int ans[N],dep[N];
int root[N],cnt;
vector<int>e[N];

void pp(int x) {
	int ls=tree[x].ls;
	int rs=tree[x].rs;
	if(tree[ls].ma>=tree[rs].ma) {
		tree[x].id=tree[ls].id;
		tree[x].ma=tree[ls].ma;
		return ;
	} else {
		tree[x].id=tree[rs].id;
		tree[x].ma=tree[rs].ma;
		return ;
	}
}

void ins(int &x,int l,int r,int pos) {
	if(!x) x=++cnt;
	if(l==r) {
		tree[x].ma++;
		tree[x].id=l;
		return ;
	}
	int mid=l+r>>1;
	if(pos<=mid) ins(tree[x].ls,l,mid,pos);
	else ins(tree[x].rs,mid+1,r,pos);
	pp(x);
}

int merge(int x,int y,int l,int r) {
	if( !x || !y) return x?x:y;
	if(l==r) {
		tree[x].ma+=tree[y].ma;
		tree[x].id=l;
		return x;
	}
	int mid=l+r>>1;
	tree[x].ls=merge(tree[x].ls,tree[y].ls,l,mid);
	tree[x].rs=merge(tree[x].rs,tree[y].rs,mid+1,r);
	pp(x);
	return x;
}

void dfs(int u,int f) {
	dep[u]=dep[f]+1;
	ins(root[u],1,N,dep[u]);
	for(auto v:e[u]) {
		if(v==f) continue;
		dfs(v,u);
		root[u]=merge(root[u],root[v],1,N);
	}
	ans[u]=tree[root[u]].id-dep[u];
}

int main() {

#ifdef xiaofan
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
#endif

	int n;
	cin>>n;
	for(int i=1; i<n; i++) {
		int u,v;
		cin>>u>>v;
		e[u].pb(v);
		e[v].pb(u);
	}
	dfs(1,0);
	for(int i=1; i<=n; i++) cout<<ans[i]<<endl;

	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
	bool IOerror=0;
	inline char nc() {
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend) {
			p1=buf;
			pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1) {
				IOerror=1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
	}
	template<class T> inline bool read(T &x) {
		bool sign=0;
		char ch=nc();
		x=0;
		for(; blank(ch); ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t) {
		return read(h)&&read(t...);
	}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1e6+1;

struct node {
	int ls,rs,ma;
} tree[N * 30];

int ans[N],dep[N];
int root[N],cnt,st[N*30],top;
vector<int>e[N];

void pp(int x) {
	int ls=tree[x].ls;
	int rs=tree[x].rs;
	tree[x].ma=max(tree[ls].ma,tree[rs].ma);
}

int newnode(){
	if(top) return st[top--];
	else return ++cnt;
}

void ins(int &x,int l,int r,int pos) {
	if(!x) x=newnode();
	if(l==r) {
		tree[x].ma++;
		return ;
	}
	int mid=l+r>>1;
	if(pos<=mid) ins(tree[x].ls,l,mid,pos);
	else ins(tree[x].rs,mid+1,r,pos);
	pp(x);
}

int merge(int x,int y,int l,int r) {
	if( !x || !y) return x?x:y;
	if(l==r) {
		tree[x].ma+=tree[y].ma;
		st[++top]=y;
		return x;
	}
	int mid=l+r>>1;
	tree[x].ls=merge(tree[x].ls,tree[y].ls,l,mid);
	tree[x].rs=merge(tree[x].rs,tree[y].rs,mid+1,r);
	pp(x);
	st[++top]=y;
	return x;
}

int query(int x,int l,int r){
	if(l==r) return l;
	int mid=l+r>>1;
	int ls=tree[x].ls;
	int rs=tree[x].rs;
	if(tree[ls].ma>=tree[rs].ma) return query(ls,l,mid);
	else return query(rs,mid+1,r);
}

void dfs(int u,int f) {
	dep[u]=dep[f]+1;
	ins(root[u],1,N,dep[u]);
	for(auto v:e[u]) {
		if(v==f) continue;
		dfs(v,u);
		root[u]=merge(root[u],root[v],1,N);
	}
	ans[u]=query(root[u],1,N)-dep[u];
}

int main() {

#ifdef xiaofan
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
#endif

	int n;
	cin>>n;
	for(int i=1; i<n; i++) {
		int u,v;
		cin>>u>>v;
		e[u].pb(v);
		e[v].pb(u);
	}
	dfs(1,0);
	for(int i=1; i<=n; i++) cout<<ans[i]<<endl;

	return 0;
}


#include <bits/stdc++.h>
#define ls x<<1
#define rs x<<1|1
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
	bool IOerror=0;
	inline char nc() {
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend) {
			p1=buf;
			pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1) {
				IOerror=1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
	}
	template<class T> inline bool read(T &x) {
		bool sign=0;
		char ch=nc();
		x=0;
		for(; blank(ch); ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t) {
		return read(h)&&read(t...);
	}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1e6+10;

vector<int>e[N];
int siz[N],dep[N],son[N],ans[N],sum[N];

void dfs1(int u,int fa) {
	dep[u]=dep[fa]+1;
	siz[u]=1;
	int maxlen=0;
	for(auto v:e[u]) {
		if(v==fa) continue;
		dfs1(v,u);
		siz[u]+=siz[v];
		if(siz[v]>siz[son[u]]) son[u]=v;
	}
}

int ma,res;

void add(int u,int fa) {
	sum[dep[u]]++;
	if(sum[dep[u]]>ma) {
		ma=sum[dep[u]];
		res=dep[u];
	}
	for(auto v:e[u]) if(v!=fa) add(v,u);
}

void del(int u,int fa) {
	sum[dep[u]]--;
	for(auto v:e[u]) if(v!=fa) del(v,u);
}

void dfs2(int u,int fa) {
	for(auto v:e[u]) {
		if(v==fa || v==son[u]) continue;
		dfs2(v,u);
		del(v,u);
	}
	if(son[u]) dfs2(son[u],u);
	sum[dep[u]]++;
	if(ma<=1){
		ma=1;
		res=dep[u];
	}
	for(auto v:e[u]) {
		if(v==son[u] || v==fa) continue;
		add(v,u);
	}
	ans[u]=res-dep[u];
}

int main() {

#ifdef xiaofan
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
#endif

	int n;
	read(n);
	for(int i=1; i<n; i++) {
		int u,v;
		read(u,v);
		e[u].pb(v);
		e[v].pb(u);
	}
	dfs1(1,0);
	dfs2(1,0);
	for(int i=1; i<=n; i++) printf("%d\n",ans[i]);

	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
	bool IOerror=0;
	inline char nc() {
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend) {
			p1=buf;
			pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1) {
				IOerror=1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
	}
	template<class T> inline bool read(T &x) {
		bool sign=0;
		char ch=nc();
		x=0;
		for(; blank(ch); ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t) {
		return read(h)&&read(t...);
	}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1e6+1;

struct node {
	int ls,rs,ma;
} tree[N * 40];

int ans[N],dep[N];
int root[N],cnt;
vector<int>e[N];

void pp(int x) {
	int ls=tree[x].ls;
	int rs=tree[x].rs;
	tree[x].ma=max(tree[ls].ma,tree[rs].ma);
}

void ins(int &x,int l,int r,int pos) {
	if(!x) x=++cnt;
	if(l==r) {
		tree[x].ma++;
		return ;
	}
	int mid=l+r>>1;
	if(pos<=mid) ins(tree[x].ls,l,mid,pos);
	else ins(tree[x].rs,mid+1,r,pos);
	pp(x);
}

int merge(int x,int y,int l,int r) {
	if( !x || !y) return x?x:y;
	if(l==r) {
		tree[x].ma+=tree[y].ma;
		return x;
	}
	int mid=l+r>>1;
	tree[x].ls=merge(tree[x].ls,tree[y].ls,l,mid);
	tree[x].rs=merge(tree[x].rs,tree[y].rs,mid+1,r);
	pp(x);
	return x;
}

int query(int x,int l,int r){
	if(l==r) return l;
	int mid=l+r>>1;
	int ls=tree[x].ls;
	int rs=tree[x].rs;
	if(tree[ls].ma>=tree[rs].ma) return query(ls,l,mid);
	else return query(rs,mid+1,r);
}

void dfs(int u,int f) {
	dep[u]=dep[f]+1;
	ins(root[u],1,N,dep[u]);
	for(auto v:e[u]) {
		if(v==f) continue;
		dfs(v,u);
		root[u]=merge(root[u],root[v],1,N);
	}
	ans[u]=query(root[u],1,N)-dep[u];
}

int main() {

#ifdef xiaofan
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
#endif

	int n;
	cin>>n;
	for(int i=1; i<n; i++) {
		int u,v;
		cin>>u>>v;
		e[u].pb(v);
		e[v].pb(u);
	}
	dfs(1,0);
	for(int i=1; i<=n; i++) cout<<ans[i]<<endl;

	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
	bool IOerror=0;
	inline char nc() {
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend) {
			p1=buf;
			pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1) {
				IOerror=1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
	}
	template<class T> inline bool read(T &x) {
		bool sign=0;
		char ch=nc();
		x=0;
		for(; blank(ch); ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t) {
		return read(h)&&read(t...);
	}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1e6+1;

struct node {
	int ls,rs,ma;
} tree[N * 4];

int ans[N],dep[N];
int root[N],cnt,st[N*4],top;
vector<int>e[N];

void pp(int x) {
	int ls=tree[x].ls;
	int rs=tree[x].rs;
	tree[x].ma=max(tree[ls].ma,tree[rs].ma);
}

int newnode(){
	if(top) return st[top--];
	else return ++cnt;
}

void ins(int &x,int l,int r,int pos) {
	if(!x) x=newnode();
	if(l==r) {
		tree[x].ma++;
		return ;
	}
	int mid=l+r>>1;
	if(pos<=mid) ins(tree[x].ls,l,mid,pos);
	else ins(tree[x].rs,mid+1,r,pos);
	pp(x);
}

int merge(int x,int y,int l,int r) {
	if( !x || !y) return x?x:y;
	if(l==r) {
		tree[x].ma+=tree[y].ma;
		st[++top]=y;
		return x;
	}
	int mid=l+r>>1;
	tree[x].ls=merge(tree[x].ls,tree[y].ls,l,mid);
	tree[x].rs=merge(tree[x].rs,tree[y].rs,mid+1,r);
	pp(x);
	st[++top]=y;
	return x;
}

int query(int x,int l,int r){
	if(l==r) return l;
	int mid=l+r>>1;
	int ls=tree[x].ls;
	int rs=tree[x].rs;
	if(tree[ls].ma>=tree[rs].ma) return query(ls,l,mid);
	else return query(rs,mid+1,r);
}

void dfs(int u,int f) {
	dep[u]=dep[f]+1;
	ins(root[u],1,N,dep[u]);
	for(auto v:e[u]) {
		if(v==f) continue;
		dfs(v,u);
		root[u]=merge(root[u],root[v],1,N);
	}
	ans[u]=query(root[u],1,N)-dep[u];
}

int main() {

#ifdef xiaofan
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
#endif

	int n;
	cin>>n;
	for(int i=1; i<n; i++) {
		int u,v;
		cin>>u>>v;
		e[u].pb(v);
		e[v].pb(u);
	}
	dfs(1,0);
	for(int i=1; i<=n; i++) cout<<ans[i]<<endl;

	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
	bool IOerror=0;
	inline char nc() {
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend) {
			p1=buf;
			pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1) {
				IOerror=1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
	}
	template<class T> inline bool read(T &x) {
		bool sign=0;
		char ch=nc();
		x=0;
		for(; blank(ch); ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t) {
		return read(h)&&read(t...);
	}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1e6+10;

struct node {
	int ls,rs,ma,id;
} tree[N * 40];

int ans[N],dep[N];
int root[N],cnt;
int st[N],top;
vector<int>e[N];

void pp(int x) {
	int ls=tree[x].ls;
	int rs=tree[x].rs;
	if(tree[ls].ma>=tree[rs].ma) {
		tree[x].id=tree[ls].id;
		tree[x].ma=tree[ls].ma;
		return ;
	} else {
		tree[x].id=tree[rs].id;
		tree[x].ma=tree[rs].ma;
		return ;
	}
}

int newnode(){
	if(top) return st[top--];
	else return ++cnt;
}

void ins(int &x,int l,int r,int pos) {
	if(!x) x=newnode();
	if(l==r) {
		tree[x].ma++;
		tree[x].id=l;
		return ;
	}
	int mid=l+r>>1;
	if(pos<=mid) ins(tree[x].ls,l,mid,pos);
	else ins(tree[x].rs,mid+1,r,pos);
	pp(x);
}

int merge(int x,int y,int l,int r) {
	if( !x || !y) return x?x:y;
	if(l==r) {
		tree[x].ma+=tree[y].ma;
		tree[x].id=l;
		st[++top]=y;
		return x;
	}
	int mid=l+r>>1;
	tree[x].ls=merge(tree[x].ls,tree[y].ls,l,mid);
	tree[x].rs=merge(tree[x].rs,tree[y].rs,mid+1,r);
	pp(x);
	st[++top]=y;
	return x;
}

void dfs(int u,int f) {
	dep[u]=dep[f]+1;
	ins(root[u],1,N,dep[u]);
	for(auto v:e[u]) {
		if(v==f) continue;
		dfs(v,u);
		root[u]=merge(root[u],root[v],1,N);
	}
	ans[u]=tree[root[u]].id-dep[u];
}

int main() {

#ifdef xiaofan
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
#endif

	int n;
	cin>>n;
	for(int i=1; i<n; i++) {
		int u,v;
		cin>>u>>v;
		e[u].pb(v);
		e[v].pb(u);
	}
	dfs(1,0);
	for(int i=1; i<=n; i++) cout<<ans[i]<<endl;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
int n,cnt,cntt,h[1000005],root[1000005],ans[1000005];
int st[4000005],top;
struct TreeNode{
	int ls,rs,maxx,maxp;
	void Clear(){
		ls=rs=maxx=maxp=0;
	}
}t[4000005];
struct Edge{
	int to,next;
}e[2000005];
struct Que{
	int k,id;
};
int NewNode(){
	if(top)return st[top--];
	return ++cntt;
}
void Add_Edge(int x,int y){
	e[++cnt].to=y;
	e[cnt].next=h[x];
	h[x]=cnt;
}
void Pushup(int p){
	int l=t[p].ls,r=t[p].rs;
	if(t[l].maxx==t[r].maxx){
		t[p].maxx=t[l].maxx,t[p].maxp=t[l].maxp;
		return ;
	}
	if(t[l].maxx>t[r].maxx){
		t[p].maxx=t[l].maxx,t[p].maxp=t[l].maxp;
	}
	else {
		t[p].maxx=t[r].maxx,t[p].maxp=t[r].maxp;
	}
}
void Update(int &p,int l,int r,int x){
	if(!p)p=NewNode();
	if(l==r){
		t[p].maxx++,t[p].maxp=l;
		return ;
	}
	int mid=(l+r)/2;
	if(x<=mid)Update(t[p].ls,l,mid,x);
	else Update(t[p].rs,mid+1,r,x);
	Pushup(p);
}
void Merge(int &p,int q,int l,int r){
	if(!p||!q){
		p=p+q;
		return ;
	}
	if(l==r){
		t[p].maxx+=t[q].maxx;
		t[q].Clear(),st[++top]=q;
		return ;
	}
	int mid=(l+r)/2;
	Merge(t[p].ls,t[q].ls,l,mid);
	Merge(t[p].rs,t[q].rs,mid+1,r);
	Pushup(p);
	t[q].Clear(),st[++top]=q;
}
void Solve(int now,int fa,int dep){
    root[now]=++cntt;
	for(int i=h[now];i;i=e[i].next){
		int y=e[i].to;
		if(y==fa)continue;
		Solve(y,now,dep+1);
		Merge(root[now],root[y],1,n);
	}
	Update(root[now],1,n,dep);
	ans[now]=t[root[now]].maxp-dep;
}
int main(){
	scanf("%d",&n);
	for(int i=2,x,y;i<=n;i++){
		scanf("%d%d",&x,&y);
		Add_Edge(x,y);
		Add_Edge(y,x);
	}
	Solve(1,0,1);
	for(int i=1;i<=n;i++)printf("%d\n",ans[i]);
	return 0;
}


#include <bits/stdc++.h>
#define ls x<<1
#define rs x<<1|1
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
	bool IOerror=0;
	inline char nc() {
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend) {
			p1=buf;
			pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1) {
				IOerror=1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
	}
	template<class T> inline bool read(T &x) {
		bool sign=0;
		char ch=nc();
		x=0;
		for(; blank(ch); ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t) {
		return read(h)&&read(t...);
	}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1e6+10;

vector<int>e[N];
int siz[N],dep[N],son[N],ans[N],sum[N],len[N];

void dfs1(int u,int fa){
	dep[u]=dep[fa]+1;
	siz[u]=1;
	int maxlen=0;
	for(auto v:e[u]){
		if(v==fa) continue;
		dfs1(v,u);
		siz[u]+=siz[v];
		if(siz[v]>siz[son[u]]) son[u]=v;
		maxlen=max(maxlen,len[v]);
	}
	len[u]=maxlen+1;
}

void add(int u,int fa){
	sum[dep[u]]++;
	for(auto v:e[u]) if(v!=fa) add(v,u);
}

void del(int u,int fa){
	sum[dep[u]]--;
	for(auto v:e[u]) if(v!=fa) del(v,u);
}

void dfs2(int u,int fa){
	for(auto v:e[u]){
		if(v==fa || v==son[u]) continue;
		dfs2(v,u);
		del(v,u);
	}
	if(son[u]) dfs2(son[u],u);
	for(auto v:e[u]){
		if(v==son[u] || v==fa) continue;
		add(v,u);
	}
	sum[dep[u]]++;
	int ma=-1,res=0;
	for(int i=dep[u];i<=dep[u]+len[u];i++) {
		if(sum[i]>ma){
			ma=sum[i];
			res=i;
		}
	}
	ans[u]=res-dep[u];
}

int main() {

#ifdef xiaofan
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
#endif

	int n;
	read(n);
	for(int i=1;i<n;i++){
		int u,v;
		read(u,v);
		e[u].pb(v);
		e[v].pb(u);
	}
	dfs1(1,0);
	dfs2(1,0);
	for(int i=1;i<=n;i++) printf("%d\n",ans[i]);

	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
	bool IOerror=0;
	inline char nc() {
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend) {
			p1=buf;
			pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1) {
				IOerror=1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
	}
	template<class T> inline bool read(T &x) {
		bool sign=0;
		char ch=nc();
		x=0;
		for(; blank(ch); ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t) {
		return read(h)&&read(t...);
	}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1e6+1;

struct node {
	int ls,rs,ma,id;
	void clear(){
		ls=0;
		rs=0;
		ma=0;
		id=0;
	}
} tree[N * 25];

int ans[N],dep[N];
int root[N],cnt,st[N*20],top;
vector<int>e[N];

void pp(int x) {
	int ls=tree[x].ls;
	int rs=tree[x].rs;
	if(tree[ls].ma>=tree[rs].ma){
		tree[x].ma=tree[ls].ma;
		tree[x].id=tree[ls].id;
		return ;
	}else{
		tree[x].ma=tree[rs].ma;
		tree[x].id=tree[rs].id;
		return ;
	}
}

int newnode(){
	if(top) return st[top--];
	else return ++cnt;
}

void ins(int &x,int l,int r,int pos) {
	if(!x) x=newnode();
	if(l==r) {
		tree[x].ma++;
		tree[x].id=l;
		return ;
	}
	int mid=l+r>>1;
	if(pos<=mid) ins(tree[x].ls,l,mid,pos);
	else ins(tree[x].rs,mid+1,r,pos);
	pp(x);
}

int merge(int x,int y,int l,int r) {
	if( !x || !y) return x?x:y;
	if(l==r) {
		tree[x].ma+=tree[y].ma;
		st[++top]=y;
		tree[y].clear();
		return x;
	}
	int mid=l+r>>1;
	tree[x].ls=merge(tree[x].ls,tree[y].ls,l,mid);
	tree[x].rs=merge(tree[x].rs,tree[y].rs,mid+1,r);
	pp(x);
	tree[y].clear();
	st[++top]=y;
	return x;
}

void dfs(int u,int f) {
	dep[u]=dep[f]+1;
	ins(root[u],1,N,dep[u]);
	for(auto v:e[u]) {
		if(v==f) continue;
		dfs(v,u);
		root[u]=merge(root[u],root[v],1,N);
	}
	ans[u]=tree[root[u]].id-dep[u];
}

int main() {

#ifdef xiaofan
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
#endif

	int n;
	read(n);
	for(int i=1; i<n; i++) {
		int u,v;
		read(u,v);
		e[u].pb(v);
		e[v].pb(u);
	}
	dfs(1,0);
	for(int i=1; i<=n; i++) printf("%d\n",ans[i]);

	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
	bool IOerror=0;
	inline char nc() {
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend) {
			p1=buf;
			pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1) {
				IOerror=1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
	}
	template<class T> inline bool read(T &x) {
		bool sign=0;
		char ch=nc();
		x=0;
		for(; blank(ch); ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t) {
		return read(h)&&read(t...);
	}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1e6+10;

struct node {
	int ls,rs,ma,id;
	void clear() {
		ls=0;
		rs=0;
		ma=0;
		id=0;
	}
} tree[N * 10];

int ans[N],dep[N];
int root[N],cnt,st[N * 10],top;
vector<int>e[N];

void pp(int x) {
	int ls=tree[x].ls;
	int rs=tree[x].rs;
	if(tree[ls].ma>=tree[rs].ma) {
		tree[x].id=tree[ls].id;
		tree[x].ma=tree[ls].ma;
		return ;
	} else {
		tree[x].id=tree[rs].id;
		tree[x].ma=tree[rs].ma;
		return ;
	}
}

int newnode() {
	if(top) return st[top--];
	else return ++cnt;
}

void ins(int &x,int l,int r,int pos) {
	if(!x) x=newnode();
	if(l==r) {
		tree[x].ma++;
		tree[x].id=l;
		return ;
	}
	int mid=l+r>>1;
	if(pos<=mid) ins(tree[x].ls,l,mid,pos);
	else ins(tree[x].rs,mid+1,r,pos);
	pp(x);
}

int merge(int x,int y,int l,int r) {
	if( !x || !y) return x?x:y;
	if(l==r) {
		tree[x].ma+=tree[y].ma;
		st[++top]=y;
		tree[y].clear();
		return x;
	}
	int mid=l+r>>1;
	tree[x].ls=merge(tree[x].ls,tree[y].ls,l,mid);
	tree[x].rs=merge(tree[x].rs,tree[y].rs,mid+1,r);
	pp(x);
	tree[y].clear();
	st[++top]=y;
	return x;
}

void dfs(int u,int f) {
	dep[u]=dep[f]+1;
	root[u]=++cnt;
	for(auto v:e[u]) {
		if(v==f) continue;
		dfs(v,u);
		root[u]=merge(root[u],root[v],1,N);
	}
	ins(root[u],1,N,dep[u]);
	ans[u]=tree[root[u]].id-dep[u];
}

int main() {

#ifdef xiaofan
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
#endif

	int n;
	read(n);
	for(int i=1; i<n; i++) {
		int u,v;
		read(u,v);
		e[u].pb(v);
		e[v].pb(u);
	}
	dfs(1,0);
	for(int i=1; i<=n; i++) printf("%d\n",ans[i]);

	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
	bool IOerror=0;
	inline char nc() {
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend) {
			p1=buf;
			pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1) {
				IOerror=1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
	}
	template<class T> inline bool read(T &x) {
		bool sign=0;
		char ch=nc();
		x=0;
		for(; blank(ch); ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t) {
		return read(h)&&read(t...);
	}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1e6+10;

struct node {
	int ls,rs,ma,id;
} tree[N * 40];

struct edge{
	int v,next;
}e[N*2];

int ans[N],dep[N];
int root[N],cnt,head[N],ecnt;

void add(int u,int v){
	e[++ecnt].v=v;
	e[ecnt].next=head[u];
	head[u]=ecnt;
}

void pp(int x) {
	int ls=tree[x].ls;
	int rs=tree[x].rs;
	if(tree[ls].ma>=tree[rs].ma) {
		tree[x].id=tree[ls].id;
		tree[x].ma=tree[ls].ma;
		return ;
	} else {
		tree[x].id=tree[rs].id;
		tree[x].ma=tree[rs].ma;
		return ;
	}
}

void ins(int &x,int l,int r,int pos) {
	if(!x) x=++cnt;
	if(l==r) {
		tree[x].ma++;
		tree[x].id=l;
		return ;
	}
	int mid=l+r>>1;
	if(pos<=mid) ins(tree[x].ls,l,mid,pos);
	else ins(tree[x].rs,mid+1,r,pos);
	pp(x);
}

int merge(int x,int y,int l,int r) {
	if( !x || !y) return x?x:y;
	if(l==r) {
		tree[x].ma+=tree[y].ma;
		tree[x].id=l;
		return x;
	}
	int mid=l+r>>1;
	tree[x].ls=merge(tree[x].ls,tree[y].ls,l,mid);
	tree[x].rs=merge(tree[x].rs,tree[y].rs,mid+1,r);
	pp(x);
	return x;
}

void dfs(int u,int f) {
	dep[u]=dep[f]+1;
	ins(root[u],1,N,dep[u]);
	for(int i=head[u];i;i=e[i].next) {
		int v=e[i].v;
		if(v==f) continue;
		dfs(v,u);
		root[u]=merge(root[u],root[v],1,N);
	}
	ans[u]=tree[root[u]].id-dep[u];
}

int main() {

#ifdef xiaofan
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
#endif

	int n;
	read(n);
	for(int i=1; i<n; i++) {
		int u,v;
		read(u,v);
		add(u,v);
		add(v,u);
	}
	dfs(1,0);
	for(int i=1; i<=n; i++) printf("%d\n",ans[i]);

	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
	bool IOerror=0;
	inline char nc() {
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend) {
			p1=buf;
			pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1) {
				IOerror=1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
	}
	template<class T> inline bool read(T &x) {
		bool sign=0;
		char ch=nc();
		x=0;
		for(; blank(ch); ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t) {
		return read(h)&&read(t...);
	}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1e6+1;

struct node {
	int ls,rs,ma;
} tree[N * 40];

int ans[N],dep[N];
int root[N],cnt;
vector<int>e[N];

void pp(int x) {
	int ls=tree[x].ls;
	int rs=tree[x].rs;
	tree[x].ma=max(tree[ls].ma,tree[rs].ma);
}

int newnode(){
	++cnt;
	tree[cnt].ls=tree[cnt].rs=tree[cnt].ma=0;
	return cnt;
}

void ins(int &x,int l,int r,int pos) {
	if(!x) x=newnode();
	if(l==r) {
		tree[x].ma++;
		return ;
	}
	int mid=l+r>>1;
	if(pos<=mid) ins(tree[x].ls,l,mid,pos);
	else ins(tree[x].rs,mid+1,r,pos);
	pp(x);
}

int merge(int x,int y,int l,int r) {
	if( !x || !y) return x?x:y;
	if(l==r) {
		tree[x].ma+=tree[y].ma;
		return x;
	}
	int mid=l+r>>1;
	tree[x].ls=merge(tree[x].ls,tree[y].ls,l,mid);
	tree[x].rs=merge(tree[x].rs,tree[y].rs,mid+1,r);
	pp(x);
	return x;
}

int query(int x,int l,int r){
	if(l==r) return l;
	int mid=l+r>>1;
	int ls=tree[x].ls;
	int rs=tree[x].rs;
	if(tree[ls].ma>=tree[rs].ma) return query(ls,l,mid);
	else return query(rs,mid+1,r);
}

void dfs(int u,int f) {
	dep[u]=dep[f]+1;
	ins(root[u],1,N,dep[u]);
	for(auto v:e[u]) {
		if(v==f) continue;
		dfs(v,u);
		root[u]=merge(root[u],root[v],1,N);
	}
	ans[u]=query(root[u],1,N)-dep[u];
}

int main() {

#ifdef xiaofan
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
#endif

	int n;
	cin>>n;
	for(int i=1; i<n; i++) {
		int u,v;
		cin>>u>>v;
		e[u].pb(v);
		e[v].pb(u);
	}
	dfs(1,0);
	for(int i=1; i<=n; i++) cout<<ans[i]<<endl;

	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
	bool IOerror=0;
	inline char nc() {
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend) {
			p1=buf;
			pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1) {
				IOerror=1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
	}
	template<class T> inline bool read(T &x) {
		bool sign=0;
		char ch=nc();
		x=0;
		for(; blank(ch); ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t) {
		return read(h)&&read(t...);
	}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1e6+1;

struct node {
	int ls,rs,ma;
} tree[N * 35];

int ans[N],dep[N];
int root[N],cnt;
vector<int>e[N];

void pp(int x) {
	int ls=tree[x].ls;
	int rs=tree[x].rs;
	tree[x].ma=max(tree[ls].ma,tree[rs].ma);
}

void ins(int &x,int l,int r,int pos) {
	if(!x) x=++cnt;
	if(l==r) {
		tree[x].ma++;
		return ;
	}
	int mid=l+r>>1;
	if(pos<=mid) ins(tree[x].ls,l,mid,pos);
	else ins(tree[x].rs,mid+1,r,pos);
	pp(x);
}

int merge(int x,int y,int l,int r) {
	if( !x || !y) return x?x:y;
	if(l==r) {
		tree[x].ma+=tree[y].ma;
		return x;
	}
	int mid=l+r>>1;
	tree[x].ls=merge(tree[x].ls,tree[y].ls,l,mid);
	tree[x].rs=merge(tree[x].rs,tree[y].rs,mid+1,r);
	pp(x);
	return x;
}

int query(int x,int l,int r){
	if(l==r) return l;
	int mid=l+r>>1;
	int ls=tree[x].ls;
	int rs=tree[x].rs;
	if(tree[ls].ma>=tree[rs].ma) return query(ls,l,mid);
	else return query(rs,mid+1,r);
}

void dfs(int u,int f) {
	dep[u]=dep[f]+1;
	ins(root[u],1,N,dep[u]);
	for(auto v:e[u]) {
		if(v==f) continue;
		dfs(v,u);
		root[u]=merge(root[u],root[v],1,N);
	}
	ans[u]=query(root[u],1,N)-dep[u];
}

int main() {

#ifdef xiaofan
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
#endif

	int n;
	cin>>n;
	for(int i=1; i<n; i++) {
		int u,v;
		cin>>u>>v;
		e[u].pb(v);
		e[v].pb(u);
	}
	dfs(1,0);
	for(int i=1; i<=n; i++) cout<<ans[i]<<endl;

	return 0;
}


#include<bits/stdc++.h>
#define MAXN 1000005
#define LL long long
#define INF (1<<27)
#define getD(p) (p?p->d:0)
#define getID(p) (p?p->id:0)
using namespace std;

int N,M;

struct Node{
    int d, id;
    Node *ls,*rs;

    void update(){
        if(getD(ls) >= getD(rs)){
            d = getD(ls);
            id = getID(ls);
        }
        else{
            d = getD(rs);
            id = getID(rs);
        }
    }
} *rt[MAXN];

Node* newNode(){
    Node *p = new Node;
    p->d = p->id = 0;
    p->ls = p->rs = 0;
    return p;
}

Node* copyNode(Node* rt){
    Node *p = newNode();
    p->d = rt->d; p->id = rt->id;
    p->ls = rt->ls; p->rs = rt->rs;
    return p;
}

Node* insert(Node* p, int l, int r, int id, int x);
Node* merge(Node* p0, Node* p1);

int ans[MAXN];
int depth[MAXN];
bool vis[MAXN];
vector<int> adj[MAXN];

void dfs(int u){
    vis[u] = 1;

    //cout<<"dfs: "<<u<<endl;
    int v;
    for(int k=0;k<adj[u].size();k++){
        v = adj[u][k];
        if(vis[v]) continue;

        depth[v] = depth[u] + 1;
        dfs(v);
        rt[u] = merge(rt[u], rt[v]);
    }

    rt[u] = insert(rt[u], 1, N, depth[u], 1);
    ans[u] = rt[u]->id;
    //cout<<"ans[u]: "<<u<<" "<<ans[u]<<endl;
}

int main(){

    scanf("%d", &N);

    int u,v;
    for(int i=1;i<N;i++){
        scanf("%d%d", &u, &v);

        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    depth[1] = 1;
    dfs(1);

    for(int i=1;i<=N;i++){
        printf("%d\n", ans[i] - depth[i]);
    }

    return 0;
}

Node* insert(Node* p, int l, int r, int id, int x){

    if(!p) p = newNode();
    if(l==r){
        p->d += x;
        p->id = id;
        return p;
    }

    int mid = (l + r)/2;
    if(id <= mid) p->ls = insert(p->ls, l, mid, id, x);
    else p->rs = insert(p->rs, mid+1, r, id, x);

    p->update();
    return p;
}

Node* merge(Node* p0, Node* p1){
    //cout<<"merge"<<endl;
    if(!p0 || !p1) return p0?p0:p1;

    Node *p = newNode();

    p->ls = merge(p0->ls, p1->ls);
    p->rs = merge(p0->rs, p1->rs);
    if(p->ls==0 && p->rs==0) p->d = getD(p0) + getD(p1), p->id = getID(p0);
    else p->update();

    delete p0; delete p1;
    return p;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
	bool IOerror=0;
	inline char nc() {
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend) {
			p1=buf;
			pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1) {
				IOerror=1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
	}
	template<class T> inline bool read(T &x) {
		bool sign=0;
		char ch=nc();
		x=0;
		for(; blank(ch); ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t) {
		return read(h)&&read(t...);
	}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1e6+1;

struct node {
	int ls,rs,ma,id;
} tree[N * 35];

int ans[N],dep[N];
int root[N],cnt;
vector<int>e[N];

void pp(int x) {
	int ls=tree[x].ls;
	int rs=tree[x].rs;
	if(tree[ls].ma>=tree[rs].ma) {
		tree[x].id=tree[ls].id;
		tree[x].ma=tree[ls].ma;
		return ;
	} else {
		tree[x].id=tree[rs].id;
		tree[x].ma=tree[rs].ma;
		return ;
	}
}

void ins(int &x,int l,int r,int pos) {
	if(!x) x=++cnt;
	if(l==r) {
		tree[x].ma++;
		tree[x].id=l;
		return ;
	}
	int mid=l+r>>1;
	if(pos<=mid) ins(tree[x].ls,l,mid,pos);
	else ins(tree[x].rs,mid+1,r,pos);
	pp(x);
}

int merge(int x,int y,int l,int r) {
	if( !x || !y) return x?x:y;
	if(l==r) {
		tree[x].ma+=tree[y].ma;
		tree[x].id=l;
		return x;
	}
	int mid=l+r>>1;
	tree[x].ls=merge(tree[x].ls,tree[y].ls,l,mid);
	tree[x].rs=merge(tree[x].rs,tree[y].rs,mid+1,r);
	pp(x);
	return x;
}

void dfs(int u,int f) {
	dep[u]=dep[f]+1;
	ins(root[u],1,N,dep[u]);
	for(auto v:e[u]) {
		if(v==f) continue;
		dfs(v,u);
		root[u]=merge(root[u],root[v],1,N);
	}
	ans[u]=tree[root[u]].id-dep[u];
}

int main() {

#ifdef xiaofan
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
#endif

	int n;
	cin>>n;
	for(int i=1; i<n; i++) {
		int u,v;
		cin>>u>>v;
		e[u].pb(v);
		e[v].pb(u);
	}
	dfs(1,0);
	for(int i=1; i<=n; i++) cout<<ans[i]<<endl;

	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
	bool IOerror=0;
	inline char nc() {
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend) {
			p1=buf;
			pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1) {
				IOerror=1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
	}
	template<class T> inline bool read(T &x) {
		bool sign=0;
		char ch=nc();
		x=0;
		for(; blank(ch); ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t) {
		return read(h)&&read(t...);
	}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1e6+10;

struct node {
	int ls,rs,ma,id;
} tree[N * 30];

int ans[N],dep[N];
int root[N],cnt;
vector<int>e[N];

void pp(int x) {
	int ls=tree[x].ls;
	int rs=tree[x].rs;
	if(tree[ls].ma>=tree[rs].ma) {
		tree[x].id=tree[ls].id;
		tree[x].ma=tree[ls].ma;
		return ;
	} else {
		tree[x].id=tree[rs].id;
		tree[x].ma=tree[rs].ma;
		return ;
	}
}

void ins(int &x,int l,int r,int pos) {
	if(!x) x=++cnt;
	if(l==r) {
		tree[x].ma++;
		tree[x].id=l;
		return ;
	}
	int mid=l+r>>1;
	if(pos<=mid) ins(tree[x].ls,l,mid,pos);
	else ins(tree[x].rs,mid+1,r,pos);
	pp(x);
}

int merge(int x,int y,int l,int r) {
	if( !x || !y) return x?x:y;
	if(l==r) {
		tree[x].ma+=tree[y].ma;
		tree[x].id=l;
		return x;
	}
	int mid=l+r>>1;
	tree[x].ls=merge(tree[x].ls,tree[y].ls,l,mid);
	tree[x].rs=merge(tree[x].rs,tree[y].rs,mid+1,r);
	pp(x);
	return x;
}

void dfs(int u,int f) {
	dep[u]=dep[f]+1;
	ins(root[u],1,N,dep[u]);
	for(auto v:e[u]) {
		if(v==f) continue;
		dfs(v,u);
		root[u]=merge(root[u],root[v],1,N);
	}
	ans[u]=tree[root[u]].id-dep[u];
}

int main() {

#ifdef xiaofan
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
#endif

	int n;
	cin>>n;
	for(int i=1; i<n; i++) {
		int u,v;
		cin>>u>>v;
		e[u].pb(v);
		e[v].pb(u);
	}
	dfs(1,0);
	for(int i=1; i<=n; i++) cout<<ans[i]<<endl;

	return 0;
}