|
|
|
|
#include <iostream>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <queue>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct TreeNode {
|
|
|
|
|
int val;
|
|
|
|
|
struct TreeNode *left;
|
|
|
|
|
struct TreeNode *right;
|
|
|
|
|
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class IsBalanced_Solution_Node {
|
|
|
|
|
public:
|
|
|
|
|
IsBalanced_Solution_Node(int min=INT_MAX,int max=INT_MIN,int high = 0,bool is_balance=true)
|
|
|
|
|
:_min(min),_max(max),_high(high),_is_balance(is_balance)
|
|
|
|
|
{}
|
|
|
|
|
int _min;
|
|
|
|
|
int _max;
|
|
|
|
|
int _high;
|
|
|
|
|
bool _is_balance;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
IsBalanced_Solution_Node my_IsBalanced_Solution(TreeNode* root) {
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>nullptr<74><72><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>Ҫ<EFBFBD><D2AA>
|
|
|
|
|
if (root == nullptr) {
|
|
|
|
|
return IsBalanced_Solution_Node();
|
|
|
|
|
}
|
|
|
|
|
IsBalanced_Solution_Node left = my_IsBalanced_Solution(root->left);
|
|
|
|
|
IsBalanced_Solution_Node right = my_IsBalanced_Solution(root->right);
|
|
|
|
|
int min = root->val;
|
|
|
|
|
int max = root->val;
|
|
|
|
|
int high = 1;
|
|
|
|
|
bool is_balance = true;
|
|
|
|
|
if (left._is_balance == false || right._is_balance == false
|
|
|
|
|
|| std::abs(left._high - right._high) > 1) {
|
|
|
|
|
is_balance = false;
|
|
|
|
|
}
|
|
|
|
|
if (left._high != 0) {
|
|
|
|
|
min = left._min < min ? left._min : ( min);
|
|
|
|
|
}
|
|
|
|
|
if (right._high != 0) {
|
|
|
|
|
max = right._max > max ? right._max : (max);
|
|
|
|
|
}
|
|
|
|
|
high = std::max(left._high, right._high) + 1;
|
|
|
|
|
return IsBalanced_Solution_Node(min, max, high, is_balance);
|
|
|
|
|
}
|
|
|
|
|
// <09>ж<EFBFBD><D0B6>Dz<EFBFBD><C7B2><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
bool IsBalanced_Solution(TreeNode* pRoot) {
|
|
|
|
|
// write code here
|
|
|
|
|
if (pRoot == nullptr) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return my_IsBalanced_Solution(pRoot)._is_balance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class lowestCommonAncestor_Node {
|
|
|
|
|
public:
|
|
|
|
|
lowestCommonAncestor_Node(TreeNode*root = nullptr)
|
|
|
|
|
:node(root){}
|
|
|
|
|
TreeNode* node;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
TreeNode* sum_node = nullptr;
|
|
|
|
|
lowestCommonAncestor_Node my_lowestCommonAncestor(TreeNode* root, int p, int q) {
|
|
|
|
|
if (root == nullptr) {
|
|
|
|
|
return lowestCommonAncestor_Node();
|
|
|
|
|
}
|
|
|
|
|
lowestCommonAncestor_Node left = my_lowestCommonAncestor(root->left,p,q);
|
|
|
|
|
lowestCommonAncestor_Node right = my_lowestCommonAncestor(root->right, p, q);
|
|
|
|
|
//<2F><>ǰ<EFBFBD>Ľӵ<C4BD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֿ<EFBFBD><D6BF>ܣ<EFBFBD>
|
|
|
|
|
//<2F><>ǰ<EFBFBD>Ľڵ<C4BD><DAB5><EFBFBD>p
|
|
|
|
|
//<2F><>ǰ<EFBFBD>Ľڵ<C4BD><DAB5><EFBFBD>q
|
|
|
|
|
//<2F><>ǰ<EFBFBD>Ľڵ<C4BD>ʲô<CAB2><C3B4><EFBFBD><EFBFBD><EFBFBD>ǣ<EFBFBD><C7A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǹ<EFBFBD><C7B9><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8>ڵ<EFBFBD>
|
|
|
|
|
//<2F><>ǰ<EFBFBD>Ľڵ<C4BD>ʲôҲ<C3B4><D2B2><EFBFBD><EFBFBD>
|
|
|
|
|
TreeNode* re = nullptr;
|
|
|
|
|
if (left.node == nullptr && right.node == nullptr) {
|
|
|
|
|
if (root->val == q || root->val == p) {
|
|
|
|
|
return lowestCommonAncestor_Node(root);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (left.node != nullptr && right.node != nullptr) {
|
|
|
|
|
sum_node = root;
|
|
|
|
|
return lowestCommonAncestor_Node();
|
|
|
|
|
}
|
|
|
|
|
if (left.node != nullptr) {
|
|
|
|
|
if (root->val == p && left.node->val == q) {
|
|
|
|
|
sum_node = root;
|
|
|
|
|
return lowestCommonAncestor_Node();
|
|
|
|
|
}
|
|
|
|
|
if (root->val == q && left.node->val == p) {
|
|
|
|
|
sum_node = root;
|
|
|
|
|
return lowestCommonAncestor_Node();
|
|
|
|
|
}
|
|
|
|
|
re = left.node;
|
|
|
|
|
}
|
|
|
|
|
if (right.node != nullptr) {
|
|
|
|
|
if (root->val == p && right.node->val == q) {
|
|
|
|
|
sum_node = root;
|
|
|
|
|
return lowestCommonAncestor_Node();
|
|
|
|
|
}
|
|
|
|
|
if (root->val == q && right.node->val == p) {
|
|
|
|
|
sum_node = root;
|
|
|
|
|
return lowestCommonAncestor_Node();
|
|
|
|
|
}
|
|
|
|
|
re = right.node;
|
|
|
|
|
}
|
|
|
|
|
return lowestCommonAncestor_Node(re);
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int aalowestCommonAncestor(TreeNode* root, int p, int q) {
|
|
|
|
|
// write code here
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD>Ŷ<EFBFBD><C5B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ㣬<DAB5><E3A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD><C7B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľڵ<C4BD>
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>𰸣<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ֱ<EFBFBD>ӵݹ<D3B5><DDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>Ĵ𰸣<C4B4>
|
|
|
|
|
// <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><C7B2>ò<EFBFBD><C3B2>鼯<EFBFBD><E9BCAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȳ<EFBFBD><C8B2>õݹ<C3B5><DDB9>ķ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľڵ㣬<DAB5>ٲ<EFBFBD><D9B2>ò<EFBFBD><C3B2>鼯<EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
sum_node = nullptr;
|
|
|
|
|
my_lowestCommonAncestor(root, p, q);
|
|
|
|
|
return sum_node->val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Union {
|
|
|
|
|
public:
|
|
|
|
|
void put(int father, int child) {
|
|
|
|
|
if (get_father(father) == -1) {
|
|
|
|
|
father_map[father] = father;
|
|
|
|
|
}
|
|
|
|
|
if (get_father(child) == -1) {
|
|
|
|
|
father_map[child] = child;
|
|
|
|
|
}
|
|
|
|
|
father_map[child] = get_father(father);
|
|
|
|
|
}
|
|
|
|
|
int get_father(int child) {
|
|
|
|
|
if (father_map.count(child) == 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
queue<int>qu;
|
|
|
|
|
int father = child;
|
|
|
|
|
qu.push(father);
|
|
|
|
|
while (father != father_map[father]) {
|
|
|
|
|
father = father_map[father];
|
|
|
|
|
qu.push(father);
|
|
|
|
|
}
|
|
|
|
|
while (!qu.empty()) {
|
|
|
|
|
father_map[qu.front()] = father;
|
|
|
|
|
qu.pop();
|
|
|
|
|
}
|
|
|
|
|
return father;
|
|
|
|
|
}
|
|
|
|
|
unordered_map<int, int>father_map;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int re = -1;
|
|
|
|
|
Union un;
|
|
|
|
|
void my_union_lowestCommonAncestor(TreeNode* root, int p, int q) {
|
|
|
|
|
if (re != -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (root != nullptr) {
|
|
|
|
|
//ÿ<><C3BF>ֻ<EFBFBD><D6BB><EFBFBD>ڵݹ<DAB5><DDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD>ͽ<F3A3ACBE><CDBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ݹ<EFBFBD><DDB9><EFBFBD>֮<EFBFBD><D6AE><EFBFBD>ͽ<EFBFBD><CDBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>ǰ<EFBFBD>ڵ<EFBFBD><DAB5>ϲ<EFBFBD>
|
|
|
|
|
my_union_lowestCommonAncestor(root->left, q, p);
|
|
|
|
|
if (root->left != nullptr) {
|
|
|
|
|
un.put(root->val, root->left->val);
|
|
|
|
|
}
|
|
|
|
|
if (root->val == q && un.get_father(p) != -1) {
|
|
|
|
|
re = un.get_father(p);
|
|
|
|
|
}
|
|
|
|
|
if(root->val == p && un.get_father(q) != -1) {
|
|
|
|
|
re = un.get_father(q);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my_union_lowestCommonAncestor(root->right, q, p);
|
|
|
|
|
if (root->right != nullptr) {
|
|
|
|
|
un.put(root->val, root->right->val);
|
|
|
|
|
}
|
|
|
|
|
if (root->val == q && un.get_father(p) != -1) {
|
|
|
|
|
re = un.get_father(p);
|
|
|
|
|
}
|
|
|
|
|
if(root->val == p && un.get_father(q) != -1) {
|
|
|
|
|
re = un.get_father(q);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD>ò<EFBFBD><C3B2>鼯<EFBFBD>ķ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>в<EFBFBD><D0B2><EFBFBD>
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int lowestCommonAncestor(TreeNode* root, int o1, int o2) {
|
|
|
|
|
// write code here
|
|
|
|
|
my_union_lowestCommonAncestor(root, o1, o2);
|
|
|
|
|
return re;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void my_Serialize(TreeNode* root, string& str) {
|
|
|
|
|
if (root == nullptr) {
|
|
|
|
|
str.append("#");
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
str.append('{'+ to_string(root->val) + '}');
|
|
|
|
|
my_Serialize(root->left, str);
|
|
|
|
|
my_Serialize(root->right, str);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
char* Serialize(TreeNode* root) {
|
|
|
|
|
string str;
|
|
|
|
|
my_Serialize(root, str);
|
|
|
|
|
|
|
|
|
|
return const_cast<char*>(str.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TreeNode* my_Deserialize( string& str, int& index) {
|
|
|
|
|
if (str[index] == '#') {
|
|
|
|
|
index++;
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
index = str.find('{', index);
|
|
|
|
|
index = index + 1;
|
|
|
|
|
int next_index = str.find('}', index);
|
|
|
|
|
|
|
|
|
|
TreeNode* cur = new TreeNode(atoi(str.substr(index, next_index).c_str()));
|
|
|
|
|
index = next_index + 1;
|
|
|
|
|
cur->left = my_Deserialize(str, index);
|
|
|
|
|
cur->right = my_Deserialize(str, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
TreeNode* Deserialize(char* str) {
|
|
|
|
|
int index = 0;
|
|
|
|
|
string ss(str);
|
|
|
|
|
return my_Deserialize( ss,index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
TreeNode* root = new TreeNode(1);
|
|
|
|
|
root->left = new TreeNode(2);
|
|
|
|
|
root->right = new TreeNode(3);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|