#include <bits/stdc++.h>
using namespace std;
struct Solver {
  vector<vector<pair<int, int>>> tree;
  vector<int> leaf;
  vector<tuple<int, int, int>> res;
  void Read() {
    int len;
    cin >> len;
    tree.resize(len);
    leaf.resize(len);
    for (int e = 0; e < len - 1; ++e) {
      int u, v, w;
      cin >> u >> v >> w;
      --u;
      --v;
      tree[u].push_back({v, w});
      tree[v].push_back({u, w});
    }
  }
  bool CheckAndRebuild(int v, int p) {
    if (tree[v].size() == 2) {
      return false;
    }
    for (int i = 0; i < tree[v].size(); ++i) {
      if (tree[v][i].first == p) {
        swap(tree[v][i], tree[v].back());
      }
      if (tree[v][i].first == p) {
        continue;
      }
      if (not CheckAndRebuild(tree[v][i].first, v)) {
        return false;
      }
    }
    if (p != -1) {
      tree[v].pop_back();
    }
    return true;
  }
  void BuildLeaves(int v) {
    if (tree[v].empty()) {
      leaf[v] = v;
      return;
    }
    for (auto&& [u, _] : tree[v]) {
      BuildLeaves(u);
    }
    leaf[v] = leaf[tree[v][0].first];
  }
  void addPath(int val, int v, int l, int ext) {
    if (l == v) {
      return;
    }
    int u;
    for (auto&& [c, _] : tree[v]) {
      if (leaf[c] != l and leaf[c] != ext) {
        u = leaf[c];
        break;
      }
    }
    res.push_back({u, ext, -val / 2});
    res.push_back({l, ext, val / 2});
    res.push_back({l, u, val / 2});
  }
  void nullifyVertex(int u, int ext) {
    if (ext == -1) {
      for (int i = 0; i < tree[u].size(); ++i) {
        auto [v, val] = tree[u][i];
        ext = leaf[tree[u][(i + 1) % tree[u].size()].first];
        addPath(val, u, leaf[v], ext);
        addPath(-val, v, leaf[v], ext);
      }
      return;
    }
    for (auto&& [v, val] : tree[u]) {
      addPath(val, u, leaf[v], ext);
      addPath(-val, v, leaf[v], ext);
    }
  }
  void nullifyTraversal(int v, int ext) {
    nullifyVertex(v, ext);
    for (int i = 0; i < tree[v].size(); ++i) {
      ext = leaf[tree[v][(i + 1) % tree[v].size()].first];
      nullifyTraversal(tree[v][i].first, ext);
    }
  }
};
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  Solver solver;
  solver.Read();
  if (solver.tree.size() == 2) {
    cout << "YES" << endl;
    cout << 1 << endl;
    cout << 1 << ' ' << 2 << ' ' << solver.tree[0][0].second << endl;
    return 0;
  }
  int root = 0;
  while (solver.tree[root].size() == 1) {
    ++root;
  }
  if (solver.CheckAndRebuild(root, -1)) {
    solver.BuildLeaves(root);
    solver.nullifyTraversal(root, -1);
    cout << "YES" << '\n';
    cout << solver.res.size() << '\n';
    for (auto&& [u, v, w] : solver.res) {
      cout << u + 1 << ' ' << v + 1 << ' ' << w << '\n';
    }
  } else {
    cout << "NO" << '\n';
  }
  return 0;
}
