Flatten Binary Tree to Linked List

来源:互联网 发布:cdma网络是什么意思 编辑:程序博客网 时间:2024/05/17 22:30

Given a binary tree, flatten it to a linked list in-place.

For example,
Given

     1    / \   2   5  / \   \ 3   4   6

The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6

  class Solution {public:    void flatten(TreeNode* root) {        if(!root) return ;        if(root->left) //不要漏        {flatten(root->left);        TreeNode *p=root->left,*q=root->left;        while(p->right) p=p->right;        p->right=root->right;        root->right=q;        root->left=NULL;}        flatten(root->right);//不要漏    }};
0 0