#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> mp;
int find_leaf(int u, int v, vector<vector<pair<int, int>>> &graph) {
  if (graph[v].size() == 1) return v;
  if (mp.count({u, v})) return mp[{u, v}];
  for (auto ite : graph[v]) {
    if (ite.first != u) {
      mp[{u, v}] = find_leaf(v, ite.first, graph);
      break;
    }
  }
  return mp[{u, v}];
}
pair<int, int> find_two_leaves(int u, int v,
                               vector<vector<pair<int, int>>> &graph) {
  if (graph[v].size() == 1) return {v, v};
  int cnt = 0, ans[2];
  for (auto ite : graph[v]) {
    if (ite.first != u) {
      ans[cnt++] = find_leaf(v, ite.first, graph);
      if (cnt == 2) break;
    }
  }
  return {ans[0], ans[1]};
}
int main() {
  int n;
  scanf("%d", &n);
  vector<vector<pair<int, int>>> graph(n + 1);
  int u, v, w;
  for (int i = 0; i < n - 1; ++i) {
    scanf("%d%d%d", &u, &v, &w);
    graph[u].push_back({v, w});
    graph[v].push_back({u, w});
  }
  for (int i = 1; i <= n; ++i) {
    if (graph[i].size() == 2) {
      printf("NO\n");
      return 0;
    }
  }
  int cnt_leaves = 0;
  for (int i = 1; i <= n; ++i)
    if (graph[i].size() == 1) ++cnt_leaves;
  printf("YES\n%d\n", (n - 1) * 4 - cnt_leaves);
  pair<int, int> res[2];
  for (int from = 1; from <= n; ++from) {
    for (auto ite : graph[from]) {
      if (from < ite.first) {
        res[0] = find_two_leaves(from, ite.first, graph);
        res[1] = find_two_leaves(ite.first, from, graph);
        printf("%d %d %d\n", res[0].first, res[1].first, ite.second / 2);
        printf("%d %d %d\n", res[0].second, res[1].second, ite.second / 2);
        if (res[0].first != res[0].second) {
          printf("%d %d %d\n", res[0].first, res[0].second, -ite.second / 2);
        }
        if (res[1].first != res[1].second) {
          printf("%d %d %d\n", res[1].first, res[1].second, -ite.second / 2);
        }
      }
    }
  }
  return 0;
}
