Convert Binary Search Tree (BST) to Sorted Doubly-Linked List

(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;
}
时间: 2024-12-25 11:37:25

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

426. Convert Binary Search Tree to Sorted Doubly Linked List

426. Convert Binary Search Tree to Sorted Doubly Linked List https://www.youtube.com/watch?v=FsxTX7-yhOw&t=1210s https://docs.google.com/document/d/1IIn5rXrUumqpxRrMKo76FbBx1ibTBDGso5rfENmkabw/edit class Solution { public Node treeToDoublyList(Node r

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* LCA(Node* root, Node* p, Node* q) { if (!root || !p || !q) return NULL; if (max(p->data, q->data) < root->data) return LCA(root->left, p,

Convert Binary Search Tree to Doubly Linked List

Convert a binary search tree to doubly linked list with in-order traversal. Have you met this question in a real interview? Yes Example Given a binary search tree: 4 / 2 5 / 1 3 return 1<->2<->3<->4<->5 Runtime: 15ms 1 /** 2 * Defi

【LeetCode】 Recover Binary Search Tree BST 中序遍历

题目:Recover Binary Search Tree <span style="font-size:18px;">/* * LeetCode: recover the binary search tree * 题目:二叉树中有两个节点被交换了位置,找出它们,并且将它们换回来,要求用o(n)的连续空间 * 知识点:1.BST树的特点:中序遍历后的节点的排列是按照非降的顺序 * 思路:按照特点中序遍历,当遇到逆序的节点则按照保存相关节点,注意分为,交换的两个点是否相邻的两

uva 1264 - Binary Search Tree(BST)

题目链接:uva 1264 - Binary Search Tree 题目大意:给定一个插入顺序,要求输出有多少种插入顺序,使得生成的BST一样. 解题思路:组合数学+BST的性质,起始左右两个子树的节点之间是没有影响的.所以逐层递推上去即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int max

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

UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树而言,仅仅要保证左边和右边顺序对就能够了,所以种数为C(左右结点总数,左结点),然后依据乘法原理乘上左右子树的情况就可以 代码: #include <cstdio> #include <cstring> typedef long long ll; const int MAXNODE =

[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 as synonymous to the previous and next pointers in a doubly-linked list. Let's take the following BST as an example, it may help you understand the p

426. Convert Binary Search Tree to Sorted Doubly Linked List - Medium

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. Let's take the following BST as an example, it may help you understand the p

Convert Sorted List to Balanced Binary Search Tree (BST)

(http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html) Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. Code: BinaryTree* sortedListToBST(ListNode *& list, int start, int