【LeetCode笔记】114.Flatten Binary Tree to Linked List(有疑惑)

题目:

  • Total Accepted: 119447
  • Total Submissions: 348844
  • Difficulty: Medium
  • Contributor: LeetCode

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

click to show hints.

Hints:

If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.

思路1:(求大神们指正)

原始的树前序遍历,存入队列,然后顺序读出,一直往树的右子树放置。这样的思路我把答案输出了,我觉得是对的呀,不知道为啥leetcode一直判断我是错的,如果有大神能来指正,真心感谢!

代码如下:

/**
 * 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 p(TreeNode* root,queue &v){
        if (root ==NULL)
            return;
        else{
            v.push(root->val);
            if(root->left!=NULL)
                p(root->left,v);
            if(root->right!=NULL)
                p(root->right,v);
        }
    }
    void flatten(TreeNode* root) {
        if(root==NULL)
            return;
        else{
            queue v;
            p(root,v);
            TreeNode* t = new TreeNode(v.front());
            TreeNode* temp = t;
            v.pop();
        
            while(!v.empty()){
                TreeNode* l = new TreeNode(v.front());
                t->right = l;
                t = t->right;
                v.pop();
            }
            root = temp;
//这一段printf是为了验证我的答案到底都不对才写的,就是把root节点打印出来看看到底是什么,结果显示是对的呀。。。
            if(root!=NULL)
                printf("root = %d\n",root->val);
            else
                printf("root null!\n");
            if(root->left!=NULL)
                printf("ok2  %d\n",root->left->val);
            else
                printf("root->left null\n");
            if(root->right!=NULL)
                printf("root->right =   %d\n",root->right->val);
            else
                printf("root->right null\n");
        
        }
    }
};
结果:


Submission Result: Wrong Answer More Details 

Input: [1,2]
Output: [1,2]
Expected: [1,null,2]
Stdout: root = 1 root->left null root->right = 2


小白不明白的是,为啥我输出的答案和标准答案是一样的,但是系统判错?

更新思路:把左子树的所有节点按照存入右子树里,然后把原来右子树的节点存入左子树最右的节点中。递归撸平右子树。
/**
 * 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==NULL)
            return;
        if(root->left!=NULL){
            TreeNode* temp = root->left;
            while(temp->right!=NULL)
                temp = temp->right;
            temp->right = root->right;
            root->right = root->left;
            root->left = NULL;
            
        }
        flatten(root->right);
    }
};






你可能感兴趣的:(算法提高)