1.将一棵二叉树按照前序遍历拆解成为一个假链表。所谓的假链表是说,用二叉树的 right 指针,来表示链表中的 next 指针
2.将一棵二叉树遍历然后存到一棵树里,这棵树是斜树,所有节点的左子树都是NULL
3./**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
void flatten(TreeNode *root) {
// write your code here
if(root==NULL) return;
if(root->left==NULL&&root->right==NULL) return;
if(root->left==NULL&&root->right!=NULL)
{
flatten(root->right);
}
if(root->left!=NULL&&root->right==NULL)
{
TreeNode *templ=root->left;
flatten(templ);
root->right=root->left;
root->left=NULL;
}
if(root->left!=NULL&&root->right!=NULL)
{
TreeNode *tempr=root->right;
TreeNode *templ=root->left;
flatten(templ);
flatten(tempr);
root->left=NULL;
root->right=templ;
while(templ->right!=NULL) templ=templ->right;templ->right=tempr;
}
}
};
4.感觉这个题比较难,想了好久都没想出来,最后还是看了同学的.