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? 

思路

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void recoverTree(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(!root)
            return ;
        vector<int> myvector;
        midOrder(root,myvector);
        int len = myvector.size();
        int first = 0;
        int i=0;
        while(i<len-1)
        {
            if(myvector[i]>=myvector[i+1])
            {
                first = i;
                break;
            }
            i++;
        }
        int second = first+1;
        int j = first+1;
        while(j<len-1)
        {
            if(myvector[j]>=myvector[j+1])
            {
                second = j+1;
                break;
            }
            j++;
        } 
        midOrderVal(root,myvector[first],myvector[second]);
        
    }
    void midOrder(TreeNode *root, vector<int> &myvector) {
        if(!root)
            return ;
        midOrder(root->left,myvector);
        myvector.push_back(root->val);
        midOrder(root->right,myvector);        
    } 
    
    void midOrderVal(TreeNode *root, int firstval, int secondval) {
        if(!root)
            return ;
        midOrderVal(root->left,firstval,secondval);
        if(root->val==firstval)
        {
            root->val = secondval;
        } 
        else if(root->val==secondval)
        {
            root->val = firstval;
        } 
        midOrderVal(root->right,firstval,secondval);        
    }     
    
};

 

最新 java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private TreeNode pre = null;
    private TreeNode first = null;
    private TreeNode second = null;
    public void recoverTree(TreeNode root) {
        if(root == null){
            return;
        }
        inorder(root);
        int temp = first.val;
        first.val = second.val;
        second.val = temp;
    }
    
    private void inorder(TreeNode root){
        if(root == null){
            return;
        }
        inorder(root.left);
        if(pre != null && pre.val >= root.val){
            if(first == null){
                first = pre;
                second = root;
            } else {
                second = root;
            }
        }
        pre = root;
        inorder(root.right);
    }
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值