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