#include <bits/stdc++.h>
using namespace std;
int grad[100010], leaf[100010], viz[100010], son[100010], dat[100010];
vector<int> g[100010], cost[100010], s1, s2, s3;
void go(int nod, int dad) {
  if (viz[nod] == 1) return;
  viz[nod] = 1;
  if (grad[nod] == 1) {
    son[dad] = nod;
    leaf[dad] = leaf[nod] = nod;
    return;
  }
  for (int i = 0; i < g[nod].size(); i++) {
    go(g[nod][i], nod);
  }
  leaf[dad] = leaf[nod];
  dat[nod] = dad;
  son[dad] = nod;
}
void doit(int dad, int nod, int val) {
  int l1 = leaf[nod], l2 = 0, l3 = 0;
  for (int i = 0; i < g[dad].size(); i++) {
    if (g[dad][i] != nod && dat[dad] != g[dad][i]) {
      l2 = leaf[g[dad][i]];
      break;
    }
  }
  for (int i = 0; i < g[dad].size(); i++) {
    if (g[dad][i] != nod && leaf[g[dad][i]] != l2 && dat[dad] != g[dad][i]) {
      l3 = leaf[g[dad][i]];
      break;
    }
  }
  if (l3 == 0) {
    for (int i = 0; i < g[dat[dad]].size(); i++) {
      if (g[dat[dad]][i] != dad && dat[dat[dad]] != g[dat[dad]][i]) {
        l3 = leaf[g[dat[dad]][i]];
        break;
      }
    }
  }
  s1.push_back(l1);
  s2.push_back(l2);
  s3.push_back(val / 2);
  s1.push_back(l1);
  s2.push_back(l3);
  s3.push_back(val / 2);
  s1.push_back(l2);
  s2.push_back(l3);
  s3.push_back(-val / 2);
}
void solve(int nod) {
  if (viz[nod] == 2) return;
  if (grad[nod] == 1) return;
  viz[nod] = 2;
  for (int i = 0; i < g[nod].size(); i++) solve(g[nod][i]);
  for (int i = 0; i < g[nod].size(); i++) {
    if (g[nod][i] != dat[nod]) {
      doit(nod, g[nod][i], cost[nod][i]);
      if (son[g[nod][i]]) doit(g[nod][i], son[g[nod][i]], -cost[nod][i]);
    }
  }
}
int main() {
  int n, x, y, val;
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> x >> y >> val;
    g[x].push_back(y);
    g[y].push_back(x);
    cost[x].push_back(val);
    cost[y].push_back(val);
    grad[x]++;
    grad[y]++;
  }
  if (n == 2) {
    cout << "YES\n";
    cout << 1 << "\n";
    cout << 1 << " " << 2 << " " << cost[1][0];
    return 0;
  }
  int nod = 0;
  for (int i = 1; i <= n; i++)
    if (grad[i] == 2) {
      cout << "NO";
      return 0;
    }
  for (int i = 1; i <= n; i++)
    if (grad[i] > 1) {
      nod = i;
      break;
    }
  go(nod, 0);
  solve(nod);
  cout << "YES\n";
  cout << s1.size() << "\n";
  for (int i = 0; i < s1.size(); i++)
    cout << s1[i] << " " << s2[i] << " " << s3[i] << "\n";
  return 0;
}
