#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
template <class T>
inline void ckmin(T &a, T b) {
  if (b < a) a = b;
}
template <class T>
inline void ckmax(T &a, T b) {
  if (b > a) a = b;
}
template <class T>
inline T sqr(T x) {
  return x * x;
}
using VI = vector<int>;
vector<VI> g;
map<pair<int, int>, int> w;
vector<pair<pair<int, int>, int>> ret;
pair<int, int> make_key(int a, int b) {
  if (a > b) swap(a, b);
  return make_pair(a, b);
}
int get_one_leave(int p, int prev) {
  for (int x : g[p])
    if (x != prev) return get_one_leave(x, p);
  return p;
}
int dfs(int p, int prev, int target, int delta) {
  if (p == target) return 1;
  for (int x : g[p])
    if (x != prev) {
      int z = dfs(x, p, target, delta);
      if (z) {
        w[make_key(p, x)] -= delta;
        return 1;
      }
    }
  return 0;
}
void update(int s, int t, int delta) {
  if (s > t) swap(s, t);
  if (delta == 0) return;
  ret.push_back(make_pair(make_pair(s, t), delta));
  dfs(s, -1, t, delta);
}
void solve(int a, int b) {
  if (((int)g[a].size()) > ((int)g[b].size())) swap(a, b);
  VI pa, pb;
  for (int x : g[a])
    if (x != b) {
      int t = get_one_leave(x, a);
      pa.push_back(t);
      if (((int)pa.size()) >= 2) break;
    }
  if (((int)pa.size()) == 0) pa.push_back(a);
  for (int x : g[b])
    if (x != a) {
      int t = get_one_leave(x, b);
      pb.push_back(t);
      if (((int)pb.size()) >= 2) break;
    }
  int z = w[make_key(a, b)];
  if (((int)pa.size()) == 1 && ((int)pb.size()) == 2) {
    update(pa[0], pb[0], z / 2);
    update(pa[0], pb[1], z / 2);
    update(pb[0], pb[1], -z / 2);
  } else {
    update(pa[0], pb[0], z / 2);
    update(pa[1], pb[1], z / 2);
    update(pa[0], pa[1], -z / 2);
    update(pb[0], pb[1], -z / 2);
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  int n;
  while (cin >> n) {
    g = vector<VI>(n);
    w.clear();
    for (int i = 0; i < (n - 1); ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      --a;
      --b;
      g[a].push_back(b);
      g[b].push_back(a);
      w[make_key(a, b)] = c;
    }
    int ok = 1;
    for (int i = 0; i < (n); ++i)
      if (((int)g[i].size()) == 2) ok = 0;
    if (ok) {
      printf("YES\n");
      if (n == 2) {
        printf("1\n");
        printf("%d %d %d\n", 1, 2, w[make_key(0, 1)]);
      } else {
        ret.clear();
        for (int i = 0; i < (n); ++i)
          for (int j : g[i])
            if (w[make_key(i, j)] != 0) solve(i, j);
        printf("%d\n", ((int)ret.size()));
        for (auto w : ret)
          printf("%d %d %d\n", w.first.first + 1, w.first.second + 1, w.second);
      }
    } else
      printf("NO\n");
    break;
  }
  return 0;
}
