dfs/on_tree/visible_tree_node
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def visible_tree_node(root: Node) -> int:
def dfs(root: Node, highest: int) -> int:
if root is None:
return 0
res = 0
if root.val >= highest:
res += 1
res += dfs(root.left, max(root.val, highest))
res += dfs(root.right, max(root.val, highest))
return res
return dfs(root, float('-inf'))
# this function builds a tree from input; you don't have to modify it
# learn more about how trees are encoded in https://algo.monster/problems/serializing_tree
def build_tree(nodes, f):
val = next(nodes)
if val == "x":
return None
left = build_tree(nodes, f)
right = build_tree(nodes, f)
return Node(f(val), left, right)
if __name__ == "__main__":
root = build_tree(iter(input().split()), int)
res = visible_tree_node(root)
print(res)
#include <algorithm>
#include <iostream>
#include <iterator>
#include <limits>
#include <sstream>
#include <string>
#include <vector>
template<typename T>
struct Node {
T val;
Node<T>* left;
Node<T>* right;
explicit Node(T val, Node<T>* left = nullptr, Node<T>* right = nullptr)
: val{val}, left{left}, right{right} {}
~Node() {
delete left;
delete right;
}
};
int dfs(Node<int>* root, int highest) {
if (root == nullptr) {
return 0;
}
int res = 0;
if (root->val >= highest) {
res++;
}
res += dfs(root->left, std::max(root->val, highest));
res += dfs(root->right, std::max(root->val, highest));
return res;
}
int visible_tree_node(Node<int>* root) {
// WRITE YOUR BRILLIANT CODE HERE
return dfs(root, std::numeric_limits<int>::min());
}
// this function builds a tree from input
// learn more about how trees are encoded in https://algo.monster/problems/serializing_tree
template<typename T, typename Iter, typename F>
Node<T>* build_tree(Iter& it, F f) {
std::string val = *it;
++it;
if (val == "x") return nullptr;
Node<T>* left = build_tree<T>(it, f);
Node<T>* right = build_tree<T>(it, f);
return new Node<T>{f(val), left, right};
}
template<typename T>
std::vector<T> get_words() {
std::string line;
std::getline(std::cin, line);
std::istringstream ss{line};
ss >> std::boolalpha;
std::vector<T> v;
std::copy(std::istream_iterator<T>{ss}, std::istream_iterator<T>{}, std::back_inserter(v));
return v;
}
int main() {
std::vector<std::string> root_vec = get_words<std::string>();
auto root_it = root_vec.begin();
Node<int>* root = build_tree<int>(root_it, [](auto s) { return std::stoi(s); });
int res = visible_tree_node(root);
std::cout << res << '\n';
}
"use strict";
class Node {
constructor(val, left = null, right = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function dfs(root, highest) {
if (root === null) {
return 0;
}
let total = 0;
if (root.val >= highest) total++;
total += dfs(root.left, Math.max(root.val, highest));
total += dfs(root.right, Math.max(root.val, highest));
return total;
}
function visibleTreeNode(root) {
return dfs(root, Number.NEGATIVE_INFINITY);
}
function buildTree(nodes, f) {
const val = nodes.next().value;
if (val === "x") return null;
const left = buildTree(nodes, f);
const right = buildTree(nodes, f);
return new Node(f(val), left, right);
}
function splitWords(s) {
return s === "" ? [] : s.split(" ");
}
function* main() {
const root = buildTree(splitWords(yield)[Symbol.iterator](), parseInt);
const res = visibleTreeNode(root);
console.log(res);
}
class EOFError extends Error {}
{
const gen = main();
const next = (line) => gen.next(line).done && process.exit();
let buf = "";
next();
process.stdin.setEncoding("utf8");
process.stdin.on("data", (data) => {
const lines = (buf + data).split("\n");
buf = lines.pop();
lines.forEach(next);
});
process.stdin.on("end", () => {
buf && next(buf);
gen.throw(new EOFError());
});
}