Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
そのツリー内の特定の要素の前に見つかった葉の数を返す関数をどのように実装しますか? ツリーを左から右に読んでいると仮定しますか?
私はそれをするために離れていましたが、それはあまり簡単ではなく、例外メカニズムを使用しています.それを行うためのエレガントな方法があると思いますか?
おそらく、ツリーを dfs し、葉を数え、すべてのノードに保存することができます。お気に入り
int count; void dfs(int x){ dfs(x->left); leafBefore[x] = count; if (x is leaf) count += 1; dfs(x->right); } count = 0; dfs(root);