19.3.2 [LeetCode 99] Recover Binary Search Tree

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

Recover the tree without changing its structure.

Example 1:

Input: [1,3,null,null,2]

   1
  /
 3
     2

Output: [3,1,null,null,2]

   3
  /
 1
     2

Example 2:

Input: [3,1,4,null,null,2]

  3
 / 1   4
   /
  2

Output: [2,1,4,null,null,3]

  2
 / 1   4
   /
  3

Follow up:

  • A solution using O(n) space is pretty straight forward.
  • Could you devise a constant space solution?

题意

给出一棵标准BST树中两个结点互换后的树,要求互换前的BST树

题解

 1 class Solution {
 2 public:
 3     TreeNode*big = NULL, *small = NULL, *tmp;
 4     TreeNode*last=NULL;
 5     void search(TreeNode*root) {
 6         if (big&&small||root==NULL)return;
 7         if (root->left)
 8             search(root->left);
 9         if (last&&root->val < last->val) {
10             if (big == NULL) {
11                 big = last;
12                 tmp = root;
13             }
14             else if (small == NULL) {
15                 small = root;
16                 return;
17             }
18         }
19         last = root;
20         if (root->right)
21             search(root->right);
22     }
23     void recoverTree(TreeNode* root) {
24         search(root);
25         if (!small)
26             small = tmp;
27         swap(big->val, small->val);
28         return;
29     }
30 };

这题应该可以直接换值吧?如果不能的话就需要再改改了

原文地址:https://www.cnblogs.com/yalphait/p/10460729.html

时间: 2024-08-26 16:52:26

19.3.2 [LeetCode 99] Recover Binary Search Tree的相关文章

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] 99. Recover Binary Search Tree 复原二叉搜索树

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Example 1: Input: [1,3,null,null,2]   1   /  3     2 Output: [3,1,null,null,2]   3   /  1     2 Example 2: Input: [3,1,4,null,null,2]

leetcode[99] Recover Binary Search Tree

题目:二叉树中有两个节点对换了值,恢复他们. 思路:因为中序遍历是有序的,如果中序遍历后的数组出现乱序,说明就是交换的.从前往后,第一次乱序的第一个,后最后一次乱序的后一个,然后把这两个值对换就好了. 想了个非常挫的办法. 先中序遍历Binary Tree Inorder Traversal,然后在数组中找到两个值,然后再中序遍历找到那两个值,交换一下.虽然AC了,但不忍直视啊. /** * Definition for binary tree * struct TreeNode { * int

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? c

leetcode -day27 Recover Binary Search Tree &amp; Interleaving String

1.  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 solut

【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 BST 中序遍历

题目:Recover Binary Search Tree <span style="font-size:18px;">/* * LeetCode: recover the binary search tree * 题目:二叉树中有两个节点被交换了位置,找出它们,并且将它们换回来,要求用o(n)的连续空间 * 知识点:1.BST树的特点:中序遍历后的节点的排列是按照非降的顺序 * 思路:按照特点中序遍历,当遇到逆序的节点则按照保存相关节点,注意分为,交换的两个点是否相邻的两

LeetCode OJ 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? Subscribe to see which compan

[Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)

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? 使用O(n)空间的话可以直接中序遍历来找问题节点. 如果是