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  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  * Definition of Doubly-ListNode
13  * class DoublyListNode {
14  * public:
15  *     int val;
16  *     DoublyListNode *next, *prev;
17  *     DoublyListNode(int val) {
18  *         this->val = val;
19            this->prev = this->next = NULL;
20  *     }
21  * }
22  */
23 class Solution {
24 public:
25     /**
26      * @param root: The root of tree
27      * @return: the head of doubly list node
28      */
29     DoublyListNode* bstToDoublyList(TreeNode* root) {
30         // Write your code here
31
32         // do inorder traversal first
33         vector<int> inorderStore;
34         inorder(root, inorderStore);
35
36         // use the values in vector to construct a double linked list
37         DoublyListNode* pre= new DoublyListNode(0);
38         return constructDoubleList(inorderStore, pre);
39     }
40 private:
41     void inorder(TreeNode* root, vector<int>& inorderStore) {
42         if(!root) return;
43         inorder(root->left, inorderStore);
44         inorderStore.push_back(root->val);
45         inorder(root->right, inorderStore);
46     }
47
48     DoublyListNode* constructDoubleList(vector<int>& inorderStore, DoublyListNode* pre) {
49         if(inorderStore.empty()) return NULL;
50
51         DoublyListNode* move = pre;
52         for (int i = 0; i < inorderStore.size(); i++) {
53             DoublyListNode* temp = new DoublyListNode(inorderStore[i]);
54             move->next = temp;
55             move = move->next;
56         }
57
58         DoublyListNode* head = pre->next;
59         head->prev = NULL;
60         return head;
61     }
62 };
时间: 2024-10-24 12:17:42

Convert Binary Search Tree to Doubly Linked List的相关文章

刷题感悟 - Convert Binary Search Tree to Doubly Linked List

将二叉查找树转化成双向链表 题目思路其实不难 ,中序遍历,然后再依次的将数据放入链表中即可 重点:新加元素前后链的配置 有个有意思的地方在于result没有赋初值 导致写代码时需要先初始化 然后删除之 . /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val =

426.&#160;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

[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

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

看起来很难,但是仔细想一下,实质就是二叉树的中序遍历的问题,中序遍历有递归和非递归(至少两种写法). 递归: class Solution { public: Node *prev; //实质是指向最后一个元素的指针 Node* treeToDoublyList(Node* root) { if (root==NULL) return NULL; Node *dummy=new Node(0,NULL,NULL); prev = dummy; inorder(root); prev->right

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: v

Convert Sorted double linked List to Binary Search Tree

Convert Sorted double linked List to Binary Search Tree In place 闫老师讲的这两个 答案 是把他 看成 single linked list了, 但是对于double linked list , 也适用 base case : at least three nodes , other wise mid.next and mid.next.next NPE NOT TESTED YET 小班文档 Solution one: 先处理 h

leetcode 109 Convert Sorted List to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * De

Convert Sorted Array to Binary Search Tree &amp; Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 方法:将有序数组转为平衡二叉树,我们知道平衡二叉树的中序遍历是有序数组,而且“中间”的元素可以作为根节点,那么构建树的时候,找出中间元素,构造根元素,然后继续重复上面的方法,构造左子树和右子树.代码如下: 1 /**