-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecoverBinarySearchTree.cpp
61 lines (59 loc) · 1.51 KB
/
RecoverBinarySearchTree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// RecoverBinarySearchTree.cpp
//
//
// Created by 廷芳 杜 on 7/13/13.
// Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//
#include <iostream>
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *firstNode = NULL;
TreeNode *secondNode = NULL;
void swap( int &a, int &b )
{
int temp = a;
a = b;
b = temp;
}
void inOrderTraversal( TreeNode *root, TreeNode * &pre )
{
if( root == NULL ) return;
inOrderTraversal( root->left, pre );
if( pre != NULL && pre->val > root->val )
{
if(firstNode == NULL && secondNode == NULL )
{
firstNode = pre;
secondNode = root;
}
else
{
secondNode = root;
}
}
pre = root;
inOrderTraversal( root->right, pre );
}
void recoverTree(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if( root == NULL ) return;
if( root->left == NULL && root->right == NULL ) return;
TreeNode *pre = NULL;
firstNode = NULL;
secondNode = NULL;
inOrderTraversal( root, pre );
if( firstNode == NULL && secondNode == NULL ) return;
swap( firstNode->val, secondNode->val );
}
};