zoukankan      html  css  js  c++  java
  • leetcode 114 二叉树展开为链表

    简介

    先进行中序遍历然后, 对指针进行迁移, 顺便对节点数据进行迁移.

    code

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
     *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
     *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
     * };
     */
    class Solution {
    public:
        vector<int> v;
        vector<TreeNode *> vd;
        unordered_map<TreeNode *, bool> m;
        void DLR(TreeNode * r){
            if(r != nullptr){
                v.push_back(r->val);
                vd.push_back(r);
                DLR(r->left);
                DLR(r->right);
            }
        }
        void flatten(TreeNode* root) {
            DLR(root);
            if(root == nullptr) return;
            for(int i=0; i<vd.size() - 1; i++) {
                vd[i]->left = nullptr;
                vd[i]->right = vd[i+1];
                vd[i]->val = v[i];
            }
            return;
        }
    };
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    linux getch()实现
    cppcheck 下载与安装(Liunx)
    apt-get 命令
    nanopb 文档
    VS调试技术
    c 单元测试 check
    GDB 调试
    GCC选项 –I,-l,-L
    作业66
    zhuoye
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/14842965.html
Copyright © 2011-2022 走看看