【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树的特点:中序遍历后的节点的排列是按照非降的顺序
 * 思路:按照特点中序遍历,当遇到逆序的节点则按照保存相关节点,注意分为,交换的两个点是否相邻的两种情况
 */
package javaTrain;

public class Train6 {
	public TreeNode firstNode = null,preNode = null,secondNode = null;
	public int preVal = Integer.MIN_VALUE;
    public void recoverTree(TreeNode root) {
        if(root == null) return;
        findInChild(root); 

        int temp = firstNode.val;
       	firstNode.val = secondNode.val;
       	secondNode.val = temp;
        return;
    }
    private void findInChild(TreeNode root){
    	if(root == null) return;
    	findInChild(root.left);
    	if(root.val < preVal){
    		if(firstNode == null){
    			firstNode = preNode;
    			secondNode = root;
    		}
    		else{
    			secondNode = root;
    			return;
    		}
    	}
    	preNode = root;
    	preVal = root.val;
    	findInChild(root.right);
    }
}  </span>
时间: 2024-10-21 12:21:11

【LeetCode】 Recover Binary Search Tree BST 中序遍历的相关文章

LeetCode: Recover Binary Search Tree [099]

[题目] 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 @ Python

原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/ 题意: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. 解题思路:这题是说一颗二叉查找树中的某两个节点被错误的交换了,需要恢复成原来的正确的二叉查找树. 算法一:思路很简单,一颗二叉查

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

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

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

PAT甲题题解-1064. Complete Binary Search Tree (30)-中序和层次遍历,水

由于是满二叉树,用数组既可以表示父节点是i,则左孩子是2*i,右孩子是2*i+1另外根据二分搜索树的性质,中序遍历恰好是从小到大排序因此先中序遍历填充节点对应的值,然后再层次遍历输出即可. 又是一道遍历的水题... #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #include <queue> using namespace std;

[C++]LeetCode: 93 Binary Search Tree Iterator (经典题,非递归的中序遍历)

题目: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and u

LeetCode详细分析 :: Recover Binary Search Tree [Tree]

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}" means? > read more on how binary tree is serialized on OJ. 这里