Recover BST

问题描述

Two elements of a binary search tree (BST) are swapped by mistake.

Recover the tree without changing its structure.

解决思路

递归思路。

中序遍历的过程中,第一个违反顺序的节点一定是错误节点的其中一个;第二个违反顺序的节点的下一个节点是另外一个错误节点。

程序

public class RecoverBST {
	private TreeNode pre = null;
	private TreeNode n1 = null;
	private TreeNode n2 = null;

	public void recoverBST(TreeNode root) {
		if (root == null) {
			return;
		}
		inorderTraversal(root);
		if (n1 != null && n2 != null) {
			int tmp = n1.val;
			n1.val = n2.val;
			n2.val = tmp;
		}
	}

	private void inorderTraversal(TreeNode root) {
		if (root==null) {
			return;
		}
		inorderTraversal(root.left);
		if (pre != null && pre.val > root.val) {
			n2 = root; // second error node‘s next
			if (n1 == null) {
				n1 = pre; // first error node
			}
		}
		pre = root;
		inorderTraversal(root.right);
	}
}

  

时间: 2024-10-17 16:54:16

Recover BST的相关文章

【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: Recover Binary Search Tree

LeetCode: 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 s

99. 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? 解题思路:本题主要是考查了Morris Traversal

LeetCode99 Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. (Hard) Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? 分析: BST的中序遍历应该是递增的,我们考

leetcode 99 Recover Binary Search Tree ----- java

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? 给定一个排序二叉树,然后任意交换了其中两个节点,要求在使用

[LeetCode][Java] 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? 题意: 二叉搜索树中有两个元素被错误的交换了 在

【Recover Binary Search Tree】cpp

题目: 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? confused what "{1,#,2,3}&

【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? 中序

LeetCode 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?二叉排序树中有两个节点被交换了,要求把树恢复成二叉排序树.最简单