【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<int> &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<int> 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);
    }
};






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值