Last active
August 29, 2015 14:03
-
-
Save Onaiplee/f327b8f603993111a267 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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