#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long n;
  cin >> n;
  vector<map<long long, long long>> adj((size_t)n);
  for (long long i = 0; i < n - 1; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    u--, v--;
    adj[u][v] = w;
    adj[v][u] = w;
  }
  set<long long> twos;
  for (long long i = 0; i < n; i++) {
    if (adj[i].size() == 2) {
      twos.insert(i);
    }
  }
  while (!twos.empty()) {
    long long u = *twos.begin();
    long long v = adj[u].begin()->first;
    long long w = adj[u].rbegin()->first;
    twos.erase(u);
    twos.erase(v);
    twos.erase(w);
    if (adj[u][v] != adj[u][w]) {
      cout << "NO" << endl;
      return 0;
    }
    adj[v][w] = adj[u][v];
    adj[w][v] = adj[u][w];
    adj[u].erase(v);
    adj[u].erase(w);
    adj[v].erase(u);
    adj[w].erase(u);
    if (adj[v].size() == 2) {
      twos.insert(v);
    }
    if (adj[w].size() == 2) {
      twos.insert(w);
    }
  }
  long long root = 0;
  while (root < n && adj[root].size() <= 1) {
    root++;
  }
  if (root == n) {
    vector<long long> v;
    for (long long i = 0; i < n; i++) {
      if (!adj[i].empty()) {
        v.push_back(i);
      }
    }
    cout << "YES" << endl;
    cout << 1 << endl;
    cout << v[0] + 1 << " " << v[1] + 1 << " " << adj[v[0]].begin()->second
         << "\n";
    return 0;
  }
  vector<pair<pair<long long, long long>, long long>> ops;
  function<vector<pair<long long, long long>>(long long, long long)> visit =
      [&](long long i, long long j) {
        if (adj[j].size() == 1) {
          return vector<pair<long long, long long>>{{j, adj[i][j]}};
        }
        if (j == root) {
          vector<vector<pair<long long, long long>>> o;
          for (pair<long long, long long> p : adj[j]) {
            long long k = p.first;
            if (k != i) {
              o.push_back(visit(j, k));
            }
          }
          for (long long k = 0; k < o.size(); k++) {
            if (k != o.size() - 1) {
              for (pair<long long, long long> &p : o[k]) {
                ops.emplace_back(
                    pair<long long, long long>{o[k + 1][0].first, p.first},
                    p.second);
                o[k + 1][0].second -= p.second;
                p.second = 0;
              }
            } else {
              for (pair<long long, long long> &p : o[k]) {
                if (p.second < 0) {
                  long long t = -p.second;
                  ops.emplace_back(
                      pair<long long, long long>{o[0][0].first, p.first}, -t);
                  o[0][0].second += t;
                  p.second += t;
                  ops.emplace_back(
                      pair<long long, long long>{o[1][0].first, p.first}, -t);
                  o[1][0].second += t;
                  p.second += t;
                  ops.emplace_back(
                      pair<long long, long long>{o[0][0].first, o[1][0].first},
                      t);
                  o[0][0].second -= t;
                  o[1][0].second -= t;
                }
              }
              long long s = 0;
              for (pair<long long, long long> p : o[k]) {
                s += p.second;
              }
              if (s != 0) {
                ops.emplace_back(
                    pair<long long, long long>{o[0][0].first, o[1][0].first},
                    -s / 2);
                o[0][0].second += s / 2;
                o[1][0].second += s / 2;
                for (pair<long long, long long> &p : o[k]) {
                  long long x = min(p.second, o[0][0].second);
                  if (x > 0) {
                    ops.emplace_back(
                        pair<long long, long long>{o[0][0].first, p.first}, x);
                    o[0][0].second -= x;
                    p.second -= x;
                  }
                  long long y = min(p.second, o[1][0].second);
                  if (y > 0) {
                    ops.emplace_back(
                        pair<long long, long long>{o[1][0].first, p.first}, y);
                    o[1][0].second -= y;
                    p.second -= y;
                  }
                }
              }
            }
          }
          return vector<pair<long long, long long>>{};
        }
        vector<pair<long long, long long>> o;
        vector<pair<long long, long long>> f;
        for (pair<long long, long long> p : adj[j]) {
          long long k = p.first;
          if (k != i) {
            vector<pair<long long, long long>> v = visit(j, k);
            o.insert(o.end(), v.begin(), v.end());
            f.push_back(v[0]);
          }
        }
        long long s = 0;
        for (pair<long long, long long> p : o) {
          s += p.second;
        }
        if (s != adj[i][j]) {
          ops.emplace_back(pair<long long, long long>{f[0].first, f[1].first},
                           -(adj[i][j] - s) / 2);
          find(o.begin(), o.end(), f[0])->second += (adj[i][j] - s) / 2;
          find(o.begin(), o.end(), f[1])->second += (adj[i][j] - s) / 2;
        }
        return o;
      };
  visit(-1, root);
  cout << "YES" << endl;
  cout << ops.size() << endl;
  for (pair<pair<long long, long long>, long long> op : ops) {
    cout << op.first.first + 1 << " " << op.first.second + 1 << " " << op.second
         << "\n";
  }
  return 0;
}
