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 = dummy->right;
        dummy->right->left = prev;
        return dummy->right;
    }

    void inorder(Node *root){
        if (root==NULL) return;
        inorder(root->left);
        prev->right = root;
        root->left = prev;
        prev = root;
        inorder(root->right);
    }
};

非递归

class Solution {
public:
    Node *prev; //实质是指向最后一个元素的指针

    Node* treeToDoublyList(Node* root) {
        if (root==NULL) return NULL;

        stack<Node *> s;
        Node *dummy=new Node(0,NULL,NULL);
        Node *p=root, *prev=dummy;

        while (!s.empty() || p!=NULL){
            while (p){
                s.push(p);
                p = p->left;
            }
            p = s.top(), s.pop();

            prev->right = p;
            p->left = prev;
            prev = p;

            p = p->right;
        }

        prev->right = dummy->right;
        dummy->right->left = prev;

        return dummy->right;
    }
};
class Solution {
public:
    Node *prev; //实质是指向最后一个元素的指针

    Node* treeToDoublyList(Node* root) {
        if (root==NULL) return NULL;

        Node *dummy=new Node(0,NULL,NULL);
        Node *prev=dummy;

        stack<Node *> s({root});
        unordered_map<Node *,int> hash;
        while (!s.empty()){
            Node *cur=s.top(); s.pop();
            if (hash[cur]==0){
                ++hash[cur];
                s.push(cur);
                if (cur->left) s.push(cur->left);
            }else{
                prev->right = cur;
                cur->left = prev;
                prev = cur;

                if (cur->right) s.push(cur->right);
            }
        }

        prev->right = dummy->right;
        dummy->right->left = prev;

        return dummy->right;
    }
};

Divide and Conquer

原文地址:https://www.cnblogs.com/hankunyan/p/9532493.html

时间: 2024-09-30 13:49:08

LeetCode 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.&#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

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】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

LeetCode OJ - Validate Binary Search Tree

题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with

leetcode 之 Recover Binary Search Tree

Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? c

【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树的特点:中序遍历后的节点的排列是按照非降的顺序 * 思路:按照特点中序遍历,当遇到逆序的节点则按照保存相关节点,注意分为,交换的两个点是否相邻的两

leetcode -day27 Recover Binary Search Tree &amp; Interleaving String

1.  Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solut

[Leetcode][BST][Validate Binary Search Tree]

判断一颗树是不是二分查找树,非常经典基础的一个算法. 我很久之前第一次做的时候,是先求出来了树的前序遍历的结果,然后判断这个数组排序后是否和排序前相同,还要判断重复虾米的,很纠结的一种做法. 后来思考了一下怎么用递归的思路做,觉得应该根据定义返回两个子树的最大值和最小值,写了一会代码,发现好麻烦,不太对的样子. 后来看了题解,发现是用了一种反向的思维,把上下界从树的顶端传下去,而不是自下而上的约束.作者太机智了. 1 /** 2 * Definition for binary tree 3 *