#include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int, int> > g[(int)(1e4 + 4)];
vector<int> ans[3];
bool used[(int)(1e4 + 4)], ok = 1;
void dfs(int);
void build(int, int, int);
void add(int, int, int);
int find_child(int);
int main() {
  int i, x, y, c;
  cin >> n;
  for (i = 1; i < n; ++i) {
    cin >> x >> y >> c;
    g[x].push_back(make_pair(y, c));
    g[y].push_back(make_pair(x, c));
  }
  if (n == 2) {
    cout << "YES\n1\n1 2 " << g[1][0].second;
    return 0;
  }
  dfs(1);
  if (!ok) {
    cout << "NO";
    return 0;
  }
  cout << "YES\n";
  cout << ans[0].size() << '\n';
  for (i = 0; i < ans[0].size(); ++i)
    cout << ans[0][i] << ' ' << ans[1][i] << ' ' << ans[2][i] << '\n';
  return 0;
}
int find_child(int x, int father) {
  for (auto it : g[x]) {
    if (it.first != father) return find_child(it.first, x);
  }
  return x;
}
void dfs(int x) {
  used[x] = 1;
  if (g[x].size() == 2) ok = 0;
  if (!ok) return;
  for (auto it : g[x])
    if (!used[it.first]) {
      build(x, it.first, it.second);
      dfs(it.first);
    }
}
void build(int x, int y, int cost) {
  int child[4], nrchild = 0;
  cost /= 2;
  if (g[x].size() == 1 || g[y].size() == 1) {
    int a = (g[x].size() == 1) ? x : y;
    int b = (a == x) ? y : x;
    for (auto it : g[b]) {
      if (it.first != a) {
        child[nrchild++] = find_child(it.first, b);
        if (nrchild == 2) break;
      }
    }
    add(a, child[0], cost);
    add(child[0], child[1], -cost);
    add(a, child[1], cost);
  } else {
    for (auto it : g[x]) {
      if (it.first != y) {
        child[nrchild++] = find_child(it.first, x);
        if (nrchild == 2) break;
      }
    }
    for (auto it : g[y]) {
      if (it.first != x) {
        child[nrchild++] = find_child(it.first, y);
        if (nrchild == 4) break;
      }
    }
    add(child[0], child[3], cost);
    add(child[1], child[2], cost);
    add(child[0], child[1], -cost);
    add(child[2], child[3], -cost);
  }
}
void add(int x, int y, int cost) {
  ans[0].push_back(x);
  ans[1].push_back(y);
  ans[2].push_back(cost);
}
