leetcode Flatten Binary Tree to Linked List

题目连接

https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ 

Flatten Binary Tree to Linked List

Description

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
	void flatten(TreeNode* root) {
        if (!root) return;
        flatten(root->left);
        flatten(root->right);
        if (root->left) {
            TreeNode* right = root->right;
            root->right = root->left;
            root->left = NULL;
            while (root->right) root = root->right;
            root->right = right;
        }
    }
};
posted @ 2015-12-09 20:29  GadyPu  阅读(131)  评论(0编辑  收藏  举报