#include <bits/stdc++.h>
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
template <class T>
using vec = std::vector<T>;
template <class T>
using vec2d = std::vector<std::vector<T>>;
bool __hack = std::ios::sync_with_stdio(false);
auto __hack1 = cin.tie(nullptr);
struct Input {
  Input(istream& in) : in(&in) {}
  template <class T>
  T next() const {
    T x;
    *in >> x;
    return x;
  }
  int ni() const { return next<int>(); }
  istream* in;
};
Input in(cin);
class Output {
 private:
  ostream* out;
  template <typename T>
  void printSingle(const T& value) {
    *out << value;
  }

 public:
  Output(ostream& out) : out(&out) {}
  inline void print() {}
  template <typename T, typename... Ts>
  inline void print(const T& f, const Ts&... args) {
    printSingle(f);
    if (sizeof...(args) != 0) {
      *out << ' ';
      print(args...);
    }
  }
  template <typename... Ts>
  inline void println(const Ts&... args) {
    print(args...);
    (*out) << '\n';
  }
  template <typename... Ts>
  inline void operator()(const Ts&... args) {
    println(args...);
  }
};
Output out(cout);
namespace template_util {
constexpr int bytecount(uint64_t x) { return x ? 1 + bytecount(x >> 8) : 0; }
template <int N>
struct bytetype {};
template <uint64_t N>
struct minimal_uint : bytetype<bytecount(N)> {};
}  // namespace template_util
void buildSimpleDfsOrder(const vec2d<int>& tree, vec<int>& height,
                         vec<int>& tIn, vec<int>& tOut, vec<int>& dfsOrder,
                         int root = 0) {
  int timer = 0;
  int n = static_cast<int>(tree.size());
  height = vector<int>(n, -1);
  dfsOrder.clear();
  dfsOrder.reserve(n);
  tIn = vector<int>(n, -1);
  tOut = vector<int>(n, -1);
  std::function<void(int, int)> dfs = [&](int v, int h) {
    height[v] = h;
    dfsOrder.push_back(v);
    tIn[v] = timer++;
    for (int to : tree[v]) {
      if (height[to] != -1) {
        continue;
      }
      dfs(to, h + 1);
    }
    tOut[v] = timer - 1;
  };
  dfs(root, 0);
}
void solve(istream& inStream, ostream& outStream) {
  in = Input(inStream);
  out = Output(outStream);
  auto n = in.ni();
  vec2d<int> g(n);
  for (int i = 0; i < (n - 1); ++i) {
    auto x = in.ni() - 1;
    auto y = in.ni() - 1;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  vec<int> height, tIn, tOut, dfsOrder;
  buildSimpleDfsOrder(g, height, tIn, tOut, dfsOrder);
  vec<set<pair<int, int>>*> countToHeight(n);
  vec<map<int, int>*> heightToCount(n);
  vec<int> result(n, 0);
  function<void(int, int)> dfs = [&](int v, int p) {
    bool isLeaf = true;
    vec<int> children;
    for (auto to : g[v]) {
      if (to == p) continue;
      dfs(to, v);
      children.push_back(to);
      isLeaf = false;
    }
    if (isLeaf) {
      countToHeight[v] = new set<pair<int, int>>();
      heightToCount[v] = new map<int, int>();
      countToHeight[v]->insert({-1, height[v]});
      (*heightToCount[v])[height[v]] = 1;
      return;
    }
    int id = 0;
    for (int i = 0; i < (children.size()); ++i) {
      if (countToHeight[children[i]]->size() >
          countToHeight[children[id]]->size()) {
        id = i;
      }
    }
    int to = children[id];
    for (int i = 0; i < (children.size()); ++i) {
      if (id == i) {
        continue;
      }
      int that = children[i];
      while (!countToHeight[that]->empty()) {
        auto cur = *(countToHeight[that]->begin());
        countToHeight[that]->erase(countToHeight[that]->begin());
        auto it = heightToCount[to]->find(cur.second);
        if (it == heightToCount[to]->end()) {
          countToHeight[to]->insert(cur);
          (*heightToCount[to])[cur.second] = -cur.first;
        } else {
          auto rev = make_pair(-it->second, it->first);
          (*heightToCount[to])[cur.second] += -cur.first;
          countToHeight[to]->erase(rev);
          rev.first += cur.first;
          countToHeight[to]->insert(rev);
        }
      }
    }
    heightToCount[v] = heightToCount[to];
    countToHeight[v] = countToHeight[to];
    countToHeight[v]->insert({-1, height[v]});
    (*heightToCount[v])[height[v]] = 1;
    int mxHeight = countToHeight[v]->begin()->second;
    int delta = mxHeight - height[v];
    result[v] = delta;
  };
  dfs(0, -1);
  for (int i = 0; i < (n); ++i) {
    out(result[i]);
  }
}
int main() {
  solve(cin, cout);
  return 0;
}
