地址:https://www.acwing.com/problem/content/87/

输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。

要求不能创建任何新的结点,只能调整树中结点指针的指向。

注意:

  • 需要返回双向链表最左侧的节点。

例如,输入下图中左边的二叉搜索树,则输出右边的排序双向链表。

解法

树的处理 一半都是递归  分为 根  树的左子树 和树的右子树

子树也是一棵树 进行递归处理  向上返回一个双链表  返回链表的头尾

最后全部转化链表

代码

/**
* 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: TreeNode* rethead = NULL; TreeNode* gleft = NULL;
TreeNode* gright = NULL; void convertInner(TreeNode* root)
{
if (NULL == root) return; if (root->val < rethead->val) rethead = root; if (root->left == NULL && root->right == NULL) {
gleft = root; gright = root;
return;
}
else if (root->left != NULL && root->right == NULL) {
convertInner(root->left);
gright->right = root;
root->left = gright;
gright = root;
}
else if (root->right != NULL && root->left == NULL) {
convertInner(root->right);
gleft->left = root;
root->right = gleft;
gleft = root;
}
else if (root->right != NULL && root->left != NULL) {
convertInner(root->left);
gright->right = root;
root->left = gright; TreeNode* leftcopy = gleft; convertInner(root->right);
gleft->left = root;
root->right = gleft;
gleft = leftcopy;
}
} TreeNode* convert(TreeNode* root) {
if (NULL == root) return NULL;
rethead = root; if (root->left == NULL && root->right == NULL) return root; if (root->left != NULL && root->right == NULL) {
convertInner(root->left);
gright->right = root;
root->left = gright;
}
else if (root->right != NULL && root->left == NULL) {
convertInner(root->right);
gleft->left = root;
root->right = gleft;
}
else if (root->right != NULL && root->left != NULL) {
convertInner(root->left);
gright->right = root;
root->left = gright; convertInner(root->right);
gleft->left = root;
root->right = gleft;
} return rethead;
} };
/**
* 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: TreeNode* rethead = NULL; TreeNode* gleft = NULL;
TreeNode* gright = NULL; void convertInner(TreeNode* root)
{
if (NULL == root) return; if (root->val < rethead->val) rethead = root; if (root->left == NULL && root->right == NULL) {
gleft = root; gright = root;
return;
}
else if (root->left != NULL && root->right == NULL) {
convertInner(root->left);
gright->right = root;
root->left = gright;
gright = root;
}
else if (root->right != NULL && root->left == NULL) {
convertInner(root->right);
gleft->left = root;
root->right = gleft;
gleft = root;
}
else if (root->right != NULL && root->left != NULL) {
convertInner(root->left);
gright->right = root;
root->left = gright; TreeNode* leftcopy = gleft; convertInner(root->right);
gleft->left = root;
root->right = gleft;
gleft = leftcopy;
}
} TreeNode* convert(TreeNode* root) {
if (NULL == root) return NULL;
rethead = root; if (root->left == NULL && root->right == NULL) return root; if (root->left != NULL && root->right == NULL) {
convertInner(root->left);
gright->right = root;
root->left = gright;
}
else if (root->right != NULL && root->left == NULL) {
convertInner(root->right);
gleft->left = root;
root->right = gleft;
}
else if (root->right != NULL && root->left != NULL) {
convertInner(root->left);
gright->right = root;
root->left = gright; convertInner(root->right);
gleft->left = root;
root->right = gleft;
} return rethead;
} };

acwing 49. 二叉搜索树与双向链表的更多相关文章

  1. 剑指Offer面试题:25.二叉搜索树与双向链表

    一.题目:二叉搜索树与双向链表 题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向.比如输入下图中左边的二叉搜索树,则输出转换之后的 ...

  2. 剑指offer 二叉搜索树与双向链表

    html, body { font-size: 15px; } body { font-family: Helvetica, "Hiragino Sans GB", 微软雅黑, & ...

  3. 剑指offer 二叉搜索树和双向链表

    剑指offer 牛客网 二叉搜索树和双向链表 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 18:58:36 2019 ...

  4. 【IT笔试面试题整理】二叉搜索树转换为双向链表

    [试题描述] 将二叉搜索树转换为双向链表 对于二叉搜索树,可以将其转换为双向链表,其中,节点的左子树指针在链表中指向前一个节点,右子树指针在链表中指向后一个节点. 思路一: 采用递归思想,对于二叉搜索 ...

  5. 《剑指offer》— JavaScript(26)二叉搜索树与双向链表

    二叉搜索树与双向链表 题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 思路 递归思想:把大问题转换为若干小问题: 由于Ja ...

  6. 《剑指offer》第三十六题(二叉搜索树与双向链表)

    // 面试题36:二叉搜索树与双向链表 // 题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求 // 不能创建任何新的结点,只能调整树中结点指针的指向. #include < ...

  7. 编程算法 - 二叉搜索树 与 双向链表 代码(C++)

    二叉搜索树 与 双向链表 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目:输入一颗二叉搜索树, 将该二叉搜索树转换成一个排序的双向链表. 要求 ...

  8. 【剑指offer】二叉搜索树转双向链表,C++实现

    原创博文,转载请注明出处! # 题目 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 二叉树节点的定义 struct TreeNod ...

  9. 剑指Offer - 九度1503 - 二叉搜索树与双向链表

    剑指Offer - 九度1503 - 二叉搜索树与双向链表2014-02-05 23:39 题目描述: 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树 ...

随机推荐

  1. Prometheus学习系列(九)之Prometheus 存储

    前言 本文来自Prometheus官网手册 和 Prometheus简介 存储 Prometheus是一个本地磁盘时间序列数据库,但也可选择与远程存储系统集成,其本地时间序列数据库以自定义格式在磁盘上 ...

  2. How to: Use the Entity Framework Code First in XAF 如何:在 XAF 中使用EF CodeFirst

    This topic demonstrates how to create a simple XAF application with a business model in a DbContext ...

  3. Electron npm install 常见错误(Windows)

    问题一:node_gyp使用版本不对 if not defined npm_config_node_gyp (node "C:\Users\Administrator\AppData\Roa ...

  4. jQuery淡入淡出轮播图实现

    大家好我是 只是个单纯的小白,这是人生第一次写博客,准备写的内容是Jquery淡入淡出轮播图实现,在此之前学习JS写的轮播图效果都感觉不怎么好,学习了jQuery里的淡入淡出效果后又写了一次轮播图效果 ...

  5. echarts 部分美化配置项使用记录

    一.图表背景色配置项,如背景颜色渐变 https://www.echartsjs.com/zh/option.html#backgroundColor 二.图表中图形的颜色,如柱状图行采用渐变颜色显示 ...

  6. CSS入门(css简介与样式汇总、CSS的使用方式和特征、CSS基础选择器和复杂选择器、边框阴影)

    一.CSS的作用 1.以统一的方式实现样式的定义 2.提高页面样式的可重用性和可维护性 3.实现了内容(HTML)和表示(CSS)的分离 HTML和CSS之间有什么关系? HTML:构建网页的结构 C ...

  7. Android项目实战之高仿网易云音乐创建项目和配置

    这一节我们来讲解创建项目:说道大家可能就会说了,创建项目还有谁不会啊,还需要讲吗,别急听我慢慢到来,肯定有你不知道的. 使用项目Android Studio创建项目我们这里就不讲解了,主要是讲解如何配 ...

  8. LeetCode刷题191203 --回溯算法

    虽然不是每天都刷,但还是不想改标题,(手动狗头 题目及解法来自于力扣(LeetCode),传送门. 算法(78): 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明: ...

  9. application context not configured for this file于spring框架使用中的原因

    spring配置文件中时常会出现这个提示,翻译过来大概意思就是没有配置该文件到项目中 于是进入到Project Structure中查看 可以很明显的看到下面有个感叹号,大概意思是下面的文件没有匹配 ...

  10. 工具推荐--刷LeetCode的神器

    本文首发于微信公众号:[坂本先生],文章地址为: https://mp.weixin.qq.com/s/vHv5hO8nils_g2VSKwu1Cg如有转载请标明出处 今天给大家安利一款快速刷Leet ...