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 problem better:

We want to transform this BST into a circular doubly linked list. Each node in a doubly linked list has a predecessor and successor. For a circular doubly linked list, the predecessor of the first element is the last element, and the successor of the last element is the first element.

The figure below shows the circular doubly linked list for the BST above. The "head" symbol means the node it points to is the smallest element of the linked list.

Specifically, we want to do the transformation in place. After the transformation, the left pointer of the tree node should point to its predecessor, and the right pointer should point to its successor. We should return the pointer to the first element of the linked list.

The figure below shows the transformed BST. The solid line indicates the successor relationship, while the dashed line means the predecessor relationship.

先通过recursion,inorder traverse得到doubly sorted linked list,再连接首尾

用dummy node处理corner case,prev从dummy开始

time: O(n), space: O(logn)

/*
// Definition for a Node.
class Node {
    public int val;
    public Node left;
    public Node right;

    public Node() {}

    public Node(int _val,Node _left,Node _right) {
        val = _val;
        left = _left;
        right = _right;
    }
};
*/
class Solution {
    Node prev = null;
    public Node treeToDoublyList(Node root) {
        if(root == null) {
            return root;
        }
        Node dummy = new Node(0, null, null);
        prev = dummy;
        inorder(root);
        Node head = dummy.right;
        head.left = prev;
        prev.right = head;
        return head;
    }

    public void inorder(Node cur) {
        if(cur == null) {
            return;
        }
        inorder(cur.left);
        prev.right = cur;
        cur.left = prev;
        prev = cur;
        inorder(cur.right);
    }
}

原文地址:https://www.cnblogs.com/fatttcat/p/10182890.html

时间: 2024-10-08 13:25:32

426. Convert Binary Search Tree to Sorted Doubly Linked List - Medium的相关文章

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

[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

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

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 List to Binary Search Tree -- leetcodeGiven a singly linked list where elements are s

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 基本思路: 中序遍历的过程,与有序链表的顺序是一一对应的. 采用中序遍历构造进树的构造. 并利用取值范围,确定,根的位置,以及子树的范围. 故需要遍历整个链表,求得总的长度. 链表长度的一半,即为根结点所在位置. 左边则为左子树的取值范围,右边即为右子树的取值范围. 同上,递归应

刷题感悟 - 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 =

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 /**

leetcode -day19 Convert Sorted List to Binary Search Tree

1.  Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 分析:将一个升序排列的链表转换为平衡二叉搜索树,采用递归的方式,先找到链表的中点,作为二叉树的根,然后递归求解左右子树. 如下: class Solution { public: Tr