Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.
For example,
Hints:
If you notice carefully in the flattened tree, each node’s right child points to the next node of a pre-order traversal.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
void flatten(struct TreeNode *root) {
struct TreeNode *tmp;
if(root == NULL) return;
while(root){
if(root->left){
tmp = root->left;
while(tmp->right)
tmp = tmp->right;
tmp->right = root->right;
root->right = root->left;
root->left = NULL;
}
root = root->right;
}
}
树的先序遍历问题,不停得将左子树添加到该树根节点右边即可!!!!
参考:http://blog.youkuaiyun.com/sbitswc/article/details/26540269