- 426. Convert Binary Search Tree
- LeetCode Convert BST to Greater
- [LeetCode]538. Convert BST to Gr
- 538. Convert BST to Greater Tree
- 538 Convert BST to Greater Tree
- [Leetcode][BST]二分搜索子树相关题目汇总/分析/总
- Leetcode-108Convert Sorted Array
- LeetCode笔记:538. Convert BST to G
- LeetCode之Convert BST to Greater
- 538. Convert BST to Greater Tree
BST转双向链表。输入一颗BST,将BST转换成一个排序的循环双向链表,要求不能创建任何新的节点,只能调整树中节点指针的指向。
对不起,这题我真理解不了
- 时间复杂度O(n),空间复杂度O(n)
- Runtime: 111 ms, faster than 12.70%
- Memory Usage: 40.6 MB, less than 15.87%
/**
* // Definition for a Node.
* function Node(val, left, right) {
* this.val = val;
* this.left = left;
* this.right = right;
* };
*/
/**
* @param {Node} root
* @return {Node}
*/
var treeToDoublyList = function(root) {
if (!root) return null;
let first = null;
let last = null;
const recursive = function(node) {
if (node) {
recursive(node.left);
if (last) {
last.right = node;
node.left = last;
} else {
first = node;
}
last = node;
recursive(node.right);
}
}
recursive(root);
last.right = first;
first.left = last;
return first;
};
网友评论