sundeepblue
3/27/2014 - 2:51 PM

form a linked list from leaves of binary tree

form a linked list from leaves of binary tree

list<TreeNode*> connect_leaves(TreeNode *root) {
    list<TreeNode*> L;
    helper(root, L);
    return L;
}

void helper(TreeNode *root, list<TreeNode*> &L) {
    if(!root->left && !root->right) {
        L.emplace(root);
        return;
    }
    helper(root->left, L);
    helper(root->right, L);
}