Given a binary tree, flatten it to a linked list in-place.
For example, 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 Hints:
If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal. |
The question has been closed for the following reason "bulk close" by 1337c0d3r 25 Oct '13, 06:41
|
/ * Definition for binary tree * struct TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void flatten(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function flat(root); }
}; |
class Solution { public:
}; |
|
|
A Java Solution:
|
|
|
|
i have problem!!!
this code sucess with vc++9.0 and g++3.4.4, but has runtime error. could anyone help me to fix this problem? |