zoukankan      html  css  js  c++  java
  • 453 将二叉树拆成链表

    原题网址:https://www.lintcode.com/problem/flatten-binary-tree-to-linked-list/description

    描述

    将一棵二叉树按照前序遍历拆解成为一个假链表。所谓的假链表是说,用二叉树的 right 指针,来表示链表中的 next 指针。

    不要忘记将左儿子标记为 null,否则你可能会得到空间溢出或是时间溢出。

    您在真实的面试中是否遇到过这个题?  

    样例

                  1
                   
         1          2
        /           
       2   5    =>    3
      /              
     3   4   6          4
                         
                          5
                           
                            6
    

    挑战

    不使用额外的空间耗费。

    标签
    二叉树
    Depth-first Search(DFS)
     
    非挑战思路
    1.前序遍历,将节点地址保存在数组中。
    2.创建链表:下标从1开始遍历数组,将当前节点的right赋值为当前数组元素,同时当前节点left赋值NULL,再用right值更新当前节点。
    AC代码:
    /**
     * 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 ;
        }
        vector<TreeNode*> tmp;
        pretrav(tmp,root);
        for (int i=1;i<(int)tmp.size();i++)
        {
            root->right=tmp[i];
            root->left=NULL;
            root=root->right;
        }
        }
        
        void pretrav(vector<TreeNode*> &tmp,TreeNode * root)
    {
        if (root==NULL)
        {
            return ;
        }
        tmp.push_back(root);
        pretrav(tmp,root->left);
        pretrav(tmp,root->right);
    }
    };

     

    挑战版思路

    参考 https://blog.csdn.net/qiujiahao123/article/details/77744270

    核心思想是将根节点的右子树挂载到左子树的最右下的节点的right上。然后将根节点左孩子赋值给右孩子,左孩子置空。

    因为左孩子为空,只需递归处理右孩子就可以了。

    AC代码: 

    /**
     * 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 ;
        }
        TreeNode * tmp=root;
        TreeNode * left=root->left;
        TreeNode * right=root->right;
        //将右子树挂载到左子树最右下节点的right上,若左子树为空不需要此操作;
        if (left)
        {
            tmp=left;
            while(tmp->right)//找到左子树最右下的节点;
            {
                tmp=tmp->right;
            }
            tmp->right=right;//将根节点右子树挂载到左子树最右的节点上;
            //根节点的左节点挂载到right处,然后root->left置空;
            root->right=left;
            root->left=NULL;
        }
        //左子树为空,只需递归右子树;
        flatten(root->right);
    
        
        }
    };

    其他方法:

    https://www.cnblogs.com/Smallhui/p/5453595.html   此文的思路同上大致相同,只不过是反过来处理。先找到左子树最左的叶子节点,将其父节点的右孩子挂载到其right上。然后左叶子赋值给父节点的右孩子,左叶子置空。向上返回,层层处理,在归去过程中解决问题。依次处理完左子树,再处理右子树。

    https://blog.csdn.net/nawuyao/article/details/51094851

    https://www.cnblogs.com/theskulls/p/5651387.html

    https://blog.csdn.net/wangyukl/article/details/70147113

     

  • 相关阅读:
    Python3高级基础(2)
    Python3面向对象基础
    Python3学习策略
    Python3基础之基本问题
    Python3基础之字典
    (转)修改IIS默认的localhost名称
    (转)IIS5.1的安装配置并发布ASP.NET网站
    (转)怎样查看局域网中自己的IP地址和其他电脑的IP地址?
    (转)sql中 in 、not in 、exists、not exists 用法和差别
    (转)union和union all的区别
  • 原文地址:https://www.cnblogs.com/Tang-tangt/p/9251592.html
Copyright © 2011-2022 走看看