#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 15;
int n, a[N], sz[N], pa[N], ch[N], t;
vector<int> g[N];
void dfs(int pos) {
  for (int i : g[pos])
    if (i != pa[pos]) {
      pa[i] = pos;
      dfs(i);
      ch[pos] = ch[i];
    }
  if (((int)(g[pos]).size()) == 1) {
    ch[pos] = pos;
  }
  sz[pos] += (((int)(g[pos]).size()) == 1);
}
vector<vector<int>> edges;
vector<vector<int>> res;
int find_leaf(int u, int pa) {
  int res = -1;
  for (int i : g[u])
    if (i != pa) {
      res = max(res, find_leaf(i, u));
    }
  if (((int)(g[u]).size()) == 1) {
    res = max(res, u);
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int x, y, c;
    cin >> x >> y >> c;
    x--, y--;
    g[x].push_back(y);
    g[y].push_back(x);
    edges.push_back({x, y, c});
  }
  for (int i = 0; i < n; i++)
    if (((int)(g[i]).size()) == 2) return !(cout << "NO" << endl);
  cout << "YES" << endl;
  if (n == 2) {
    cout << 1 << endl << 1 << " " << 2 << " " << edges[0][2] << endl;
    return 0;
  }
  for (auto it : edges) {
    int u = it[0], v = it[1], c = it[2];
    vector<int> l, r;
    for (int i : g[u]) {
      if (i != v) l.push_back(find_leaf(i, u));
      if (((int)(l).size()) == 2) break;
    }
    if (!((int)(l).size())) l.push_back(u);
    for (int i : g[v]) {
      if (i != u) r.push_back(find_leaf(i, v));
      if (((int)(r).size()) == 2) break;
    }
    if (!(((int)(r).size()))) r.push_back(v);
    if (((int)(l).size()) > ((int)(r).size())) swap(l, r);
    assert(((int)(r).size()) == 2);
    if (((int)(l).size()) == 1) {
      res.push_back({l[0] + 1, r[0] + 1, c / 2});
      res.push_back({l[0] + 1, r[1] + 1, c / 2});
      res.push_back({r[0] + 1, r[1] + 1, -c / 2});
    } else {
      res.push_back({l[0] + 1, r[0] + 1, c / 2});
      res.push_back({l[1] + 1, r[1] + 1, c / 2});
      res.push_back({l[0] + 1, l[1] + 1, -c / 2});
      res.push_back({r[0] + 1, r[1] + 1, -c / 2});
    }
  }
  cout << ((int)(res).size()) << "\n";
  for (auto v : res) {
    for (auto it : v) cout << it << " ";
    cout << "\n";
  }
}
