(http://leetcode.com/2010/11/convert-binary-search-tree-bst-to.html)

Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list.

Code:

void treeToDoublyList(Node *p, Node *& prev, Node *& head)
{
if (!p)
return;
treeToDoublyList(p->left, prev, head); p->left = prev;
if (prev)
prev->right = p;
else
head = p;
prev = p; treeToDoublyList(p->right, prev, head);
} Node* treeToDoublyList(Node* root)
{
Node* prev = NULL;
Node* head = NULL;
treeToDoublyList(root, prev, head);
head->left = prev;
prev->right= head; return head;
}

Convert Binary Search Tree (BST) to Sorted Doubly-Linked List的更多相关文章

  1. Lowest Common Ancestor of a Binary Search Tree (BST)

    Given a binary search tree(BST), find the lowest common ancestor of two given nodes in the BST. Node ...

  2. PAT 1099 Build A Binary Search Tree[BST性质]

    1099 Build A Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary ...

  3. Convert Binary Search Tree to Doubly Linked List

    Convert a binary search tree to doubly linked list with in-order traversal. Example Given a binary s ...

  4. UVA 1264 - Binary Search Tree(BST+计数)

    UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树 ...

  5. 426. Convert Binary Search Tree to Sorted Doubly Linked List把bst变成双向链表

    [抄题]: Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right po ...

  6. [LeetCode] Convert Binary Search Tree to Sorted Doubly Linked List 将二叉搜索树转为有序双向链表

    Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers ...

  7. LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List

    原题链接在这里:https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/ 题目: C ...

  8. 【LeetCode】426. Convert Binary Search Tree to Sorted Doubly Linked List 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  9. [leetcode]426. Convert Binary Search Tree to Sorted Doubly Linked List二叉搜索树转有序双向链表

    Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers ...

随机推荐

  1. iOS 性能测试 - FBMemoryProfiler

    FBMemoryProfiler 是Facebook开源的一款用于分析iOS内存使用和检测循环引用的工具库. 脑补:http://www.cocoachina.com/ios/20160421/159 ...

  2. 获取枚举Name,Value,Description两种方法

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  3. hdu 3068 最长回文(manachar求最长回文子串)

    题目连接:hdu 3068 最长回文 解题思路:通过manachar算法求最长回文子串,如果用遍历的话绝对超时. #include <stdio.h> #include <strin ...

  4. mybatisnet轻量级ORM框架

    https://code.google.com/p/mybatisnet/source/checkout http://blog.csdn.net/arvinstudy/article/details ...

  5. 使用prototype扩展的JavaScript常用函数库

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...

  6. 对List对象按照某个成员变量进行排序

    /** * 对List对象按照某个成员变量进行排序 * @param list List对象 * @param sortField 排序的属性名称 * @param sortMode 排序方式:ASC ...

  7. jquery.validate校验文件使用说明

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一导入js库<script src="../js/ ...

  8. 提取 ECharts 中的svg地图信息

    地图的需求还是蛮大的,全国都要自己画的话,还是需要投入比较大的人力. ECharts中有地图,那我们能不能把里面的地图文件提取出来呢,主要逻辑在map.js中. 看源代码发现,ECharts中地图信息 ...

  9. VHDL testbench 例子,包含向文件中写数据

      LIBRARY ieee; USE ieee.std_logic_1164.ALL; use std.textio.all; use ieee.std_logic_textio.all;   EN ...

  10. IP校验和

    #include <stdio.h> #include <unistd.h> #include <linux/if_ether.h> #include <li ...