#include <bits/stdc++.h>
using namespace std;
int N, M, eu[600005], ev[600005], ew[600005], edel[600005], deg[300005];
vector<int> G[300005];
int Q, A1[300005], A2[300005], A3[300005];
inline void ad(int u, int v, int x) { ++Q, A1[Q] = u, A2[Q] = v, A3[Q] = x; }
int lef[300005], lcnt;
int del[300005];
int getany(int u, int f) {
  for (auto i : G[u]) {
    int v = eu[i] == u ? ev[i] : eu[i];
    if (v != f) return getany(v, u);
  }
  return u;
}
void sub(int u, int f, int x) {
  for (auto i : G[u]) {
    int v = eu[i] == u ? ev[i] : eu[i];
    if (v != f) {
      ew[i] -= x, sub(v, u, x);
      return;
    }
  }
}
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= (N - 1); ++i) {
    scanf("%d%d%d", &eu[i], &ev[i], &ew[i]);
    ++deg[eu[i]], ++deg[ev[i]];
  }
  M = N - 1;
  for (int i = 1; i <= (N); ++i)
    if (deg[i] == 2) return puts("NO"), 0;
  puts("YES");
  for (int i = 1; i <= (N); ++i)
    if (deg[i] == 1) lef[++lcnt] = i;
  for (int i = 1; i <= (lcnt - 2); ++i) {
    for (int i = 1; i <= (N); ++i) G[i].clear(), deg[i] = 0;
    for (int j = 1; j <= (M); ++j)
      if (!edel[j]) G[eu[j]].push_back(j), ++deg[eu[j]];
    for (int j = 1; j <= (M); ++j)
      if (!edel[j]) G[ev[j]].push_back(j), ++deg[ev[j]];
    int u = lef[i], edg = G[u][0];
    int v = eu[edg] == u ? ev[edg] : eu[edg], x = ew[edg];
    edel[edg] = 1, del[u] = 1;
    if (deg[v] >= 4) {
      int a = getany(v, u);
      ad(u, a, x);
      sub(v, u, x);
    } else {
      int e1 = 0, e2 = 0;
      for (auto j : G[v])
        if (j != edg) {
          if (!e1)
            e1 = j;
          else
            e2 = j;
        }
      int w1 = ew[e1], w2 = ew[e2];
      int v1 = eu[e1] == v ? ev[e1] : eu[e1];
      int v2 = eu[e2] == v ? ev[e2] : eu[e2];
      int a1 = getany(v1, v), a2 = getany(v2, v);
      int A = (x + w1 - w2) / 2, B = (x - w1 + w2) / 2;
      ad(u, a1, A), ad(u, a2, B);
      sub(v1, v, A), sub(v2, v, B);
      del[v] = 1, edel[e1] = edel[e2] = 1;
      ++M, eu[M] = v1, ev[M] = v2, ew[M] = w1 - A;
    }
  }
  for (int j = 1; j <= (M); ++j)
    if (!edel[j]) ad(eu[j], ev[j], ew[j]);
  printf("%d\n", Q);
  for (int i = 1; i <= (Q); ++i) printf("%d %d %d\n", A1[i], A2[i], A3[i]);
  return 0;
}
