二叉搜索树交换节点-LintCode

204 篇文章 0 订阅

在一棵二叉搜索树中, 只有两个节点是被交换的. 找到这些节点并交换, 如果没有节点被交换就返回原来的树的根节点.

样例
给一棵BST:

    4
   / \
  5   2
 / \
1   3

返回

    4
   / \
  2   5
 / \
1   3
#ifndef C691_H
#define C691_H
#include<iostream>
#include<vector>
using namespace std;
class TreeNode{
public:
    int val;
    TreeNode *left, *right;
    TreeNode(int val)
    {
        this->val = val;
        this->left = this->right = NULL;
    }
};
//中序遍历,得到序列,在其中找到逆序的点
class Solution {
public:
    /*
    * @param : the given tree
    * @return: the tree after swapping
    */
    TreeNode * bstSwappedNode(TreeNode * root) {
        // write your code here
        vector<int> res;
        inOrder(root, res);
        if (res.empty() || res.size() == 1)
            return root;
        int len = res.size();
        int i = 0, j = len - 1;
        bool flag1 = false, flag2 = false;
        while (i<j)
        {
            if (res[j] < res[j - 1])
            {
                flag1 = true;
            }
            else
                --j;
            if (res[i]>res[i + 1])
            {
                flag2 = true;
            }
            else
                ++i;
            if (flag1&&flag2)
                break;
        }
        if (!(flag1&&flag2))
            return root;
        createTree(root, res[i], res[j]);
        return root;
    }
    void createTree(TreeNode *root, int i, int j)
    {
        if (root == NULL)
            return;
        createTree(root->left, i, j);
        if (root->val == i)
            root->val = j;
        else if (root->val == j)
            root->val = i;
        createTree(root->right, i, j);
    }
    /*
    bool isBST(TreeNode* root)
    {
        vector<int> res;
        inOrder(root, res);
        if (res.empty() || res.size() == 1)
            return true;
        for (int i = 0; i < res.size()-1; ++i)
        {
            if (res[i]>=res[i + 1])
            {
                return false;
            }
        }
        return true;
    }
    */
    void inOrder(TreeNode *root,vector<int> &res)
    {
        if (root == NULL)
            return;
        inOrder(root->left, res);
        res.push_back(root->val);
        inOrder(root->right, res);
    }
};
//定义两个节点node1,node2为要交换的节点
//中序遍历,对于逆序的节点对,node1取逆序对的前一个节点,node2取逆序对的后一个节点
class Solution2 {
public:
    /**
    * @param root: the given tree
    * @return: the tree after swapping
    */
    TreeNode * bstSwappedNode(TreeNode * root) {
        // write your code here
        if (!root)
            return root;
        node1 = node2 = pre = NULL;
        inorder(root);
        if (node1&&node2)
            swap(node1->val, node2->val);
        return root;
    }
    void inorder(TreeNode *root)
    {
        if (!root)
            return;
        inorder(root->left);
        if (pre && pre->val > root->val)
        {
            if (!node1)
            {
                node1 = pre;
                node2 = root;
            }
            else
                node2 = root;
        }
        pre = root;
        inorder(root->right);
    }
    TreeNode *node1, *node2, *pre;
};
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值