给你一棵二叉搜索树,请你 按中序遍历 将其重新排列为一棵递增顺序搜索树,使树中最左边的节点成为树的根节点,并且每个节点没有左子节点,只有一个右子节点。
输入:root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
输出:[1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
题解:
中序遍历一遍,记录 head 和 tail,每次新遍历到节点都赋值个 tail.right,tail 指向新节点。
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var increasingBST = function(root) {
let head = null
let tail = null
function dfs (node) {
if (!node) {
return
}
dfs(node.left)
if (!head) {
head = node
tail = node
} else {
tail.right = node
tail = node
tail.left = null
}
dfs(node.right)
}
dfs(root)
return head
};