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 root) {
      Node head = null;
      Node prev = null;

      Stack<Node> stack = new Stack<>(); // Stack<Node> stack = new LinkedList<>(); why its not correct
      while(root != null || !stack.isEmpty()){
        while(root != null){
          stack.push(root);
          root = root.left;
        }

        root = stack.pop();
        root.left = prev;
        if(prev != null){
          prev.right = root;
        }else{
          head = root;
        }
        Node right = root.right;
        head.left = root;
        root.right = head;
        prev = root;
        root = right;
      }
      return head;

    }
}

原文地址:https://www.cnblogs.com/tobeabetterpig/p/9450664.html

时间: 2024-08-01 16:00:11

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

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