#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
int n;
vector<vector<pii>> adj;
vector<vector<int>> leaves;
vector<vector<int>> ans;
int root;
int dfs1(int v, int p) {
  if (adj[v].size() == 1 && p > 0) {
    leaves[v].push_back(v);
    return v;
  }
  int next_leaf = 0;
  for (auto u : adj[v]) {
    if (u.first == p) continue;
    next_leaf = dfs1(u.first, v);
    leaves[v].push_back(next_leaf);
  }
  return next_leaf;
}
void add_path(int r, int s, int z) {
  if (r == s) return;
  if (adj[s].size() == 1) {
    ans.push_back({r, s, z});
    return;
  }
  if (leaves[s].size() < 2) exit(5);
  ans.push_back({r, leaves[s][0], z / 2});
  ans.push_back({r, leaves[s][1], z / 2});
  ans.push_back({leaves[s][0], leaves[s][1], -(z / 2)});
}
void add_edge(int a, int b, int z) {
  add_path(root, b, z);
  add_path(root, a, -z);
}
void dfs2(int v, int p) {
  for (auto u : adj[v]) {
    if (u.first == p) continue;
    add_edge(v, u.first, u.second);
    dfs2(u.first, v);
  }
}
int main() {
  cin >> n;
  adj.resize(n + 1);
  leaves.resize(n + 1);
  int x, y, z;
  for (int i = 0; i < n - 1; ++i) {
    cin >> x >> y >> z;
    adj[x].push_back({y, z});
    adj[y].push_back({x, z});
  }
  bool ok = true;
  for (int i = 1; i <= n; ++i) ok &= (adj[i].size() != 2);
  for (int i = 1; i <= n; ++i)
    if (adj[i].size() == 1) root = i;
  if (!ok) {
    cout << "NO" << endl;
    return 0;
  }
  cout << "YES" << endl;
  dfs1(root, 0);
  dfs2(root, 0);
  cout << ans.size() << endl;
  for (const auto& v : ans) {
    for (auto u : v) cout << u << " ";
    cout << endl;
  }
}
