Skip to content

Instantly share code, notes, and snippets.

@Onaiplee
Last active August 29, 2015 14:03
Show Gist options
  • Save Onaiplee/f327b8f603993111a267 to your computer and use it in GitHub Desktop.
Save Onaiplee/f327b8f603993111a267 to your computer and use it in GitHub Desktop.
void print_external_nodes(shared_ptr<BinaryTree> root) {
if (!root) {
return;
}
left_dfs(root->left, true);
right_dfs(root->right, true);
}
void left_dfs(shared_ptr<BinaryTree> n, bool is_ext) {
if (!n) {
return;
}
if (is_ext || (!n->left && !n->right)) {
cout << n->data << endl;
}
left_dfs(n->left, is_ext);
left_dfs(n->right, is_ext && !n->left);
}
void rigt_dfs(shared_ptr<BinaryTree> n, bool is_ext) {
if (!n) {
return;
}
right_dfs(n->left, is_ext && !n->right);
right_dfs(n->right, is_ext);
if (is_ext || (!n->left && !n->right)) {
cout << n->data << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment