Deleting all Leaves from a Binary Tree - Binary Tree Problems
Solution: By using post-order traversal we can solve this problem (other traversals would also work).
01 | struct BinaryTreeNode* removeLeaves( struct BinaryTreeNode* root) { |
04 | if (root->left == NULL && root->right == NULL) |
11 | root->left = removeLeaves(root->left); |
12 | root->right = removeLeaves(root->right); |
Time Complexity: O(n). Where is numbe of nodes in tree.
Read full article from Deleting all Leaves from a Binary Tree - Binary Tree Problems