#include <bits/stdc++.h>
using namespace std;
vector<int> ch[100010];
struct Edge {
  int to, old, v;
} e[100010];
int n, fa[100010], w[100010], tot, l[100010], rt, d[100010];
inline void Lnk(int x, int y, int z) {
  e[++tot].to = y, e[tot].old = l[x], l[x] = tot, e[tot].v = z;
}
inline void Dfs(int x, int y) {
  fa[x] = y;
  for (int i = l[x]; i; i = e[i].old)
    if (e[i].to != y) {
      ch[x].push_back(e[i].to);
      w[e[i].to] = e[i].v;
      Dfs(e[i].to, x);
    }
}
int N, sm[100010], lf[100010];
struct Node {
  int x, y, w;
} q[100010 * 3];
inline void Work(int x) {
  if (d[x] == 1 && x != rt) {
    q[++N].x = rt, q[N].y = x, q[N].w = w[x];
    sm[x] = w[x], lf[x] = x;
    return;
  }
  for (int i = 0; i < ch[x].size(); i++) {
    Work(ch[x][i]);
    sm[x] += sm[ch[x][i]];
  }
  lf[x] = lf[ch[x][0]];
  if (sm[x] != w[x] && x != rt) {
    if (ch[x].size() < 2) {
      puts("NO");
      exit(0);
    }
    q[++N].x = lf[ch[x][0]];
    q[N].y = lf[ch[x][1]], q[N].w = (sm[x] - w[x]) / 2;
    q[++N].x = lf[ch[x][0]];
    q[N].y = rt, q[N].w = (w[x] - sm[x]) / 2;
    q[++N].x = lf[ch[x][1]];
    q[N].y = rt, q[N].w = (w[x] - sm[x]) / 2;
    sm[x] = w[x];
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    Lnk(x, y, z), Lnk(y, x, z), d[x]++, d[y]++;
  }
  for (int i = 1; i <= n; i++)
    if (d[i] == 1) rt = i;
  Dfs(rt, 0);
  Work(rt);
  puts("YES");
  printf("%d\n", N);
  for (int i = 1; i <= N; i++) printf("%d %d %d\n", q[i].x, q[i].y, q[i].w);
}
