二叉搜索树-99. 恢复二叉搜索树

69 阅读1分钟

99. 恢复二叉搜索树

给你二叉搜索树的根节点 root ,该树中的 恰好 两个节点的值被错误地交换。请在不改变其结构的情况下,恢复这棵树

 

示例 1:

输入: root = [1,3,null,null,2]
输出: [3,1,null,null,2]
解释: 3 不能是 1 的左孩子,因为 3 > 1 。交换 13 使二叉搜索树有效。

示例 2:

输入: root = [3,1,4,null,null,2]
输出: [2,1,4,null,null,3]
解释: 2 不能在 3 的右子树中,因为 2 < 3 。交换 23 使二叉搜索树有效。

 

提示:

  • 树上节点的数目在范围 [2, 1000] 内
  • -231 <= Node.val <= 231 - 1

 

进阶: 使用 O(n) 空间复杂度的解法很容易实现。你能想出一个只使用 O(1) 空间的解决方案吗?

/**
 * 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 {void} Do not return anything, modify root in-place instead.
 */
var recoverTree = function(root) {
	let p = root;
	const stack = [];
	let first;
	let second;
	let pre = null;
	while(p || stack.length) {
		while(p) {
			stack.push(p);
			p = p.left;
		}
		const node = stack.pop();
		if (pre && pre.val > node.val) {
			if (!first) {
				first = pre;
			} 
			second = node;
		}
		pre = node;
		p = node.right;
	}

	const temp = first.val;
	first.val = second.val;
	second.val = temp;
	return root;
};