#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
class Solution {
public:
ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2);
int GetNumberOfK(vector<int> data, int k);
int firstofk(vector<int> data, int k, int begin, int end);
int lastofk(vector<int> data, int k, int begin, int end);
int TreeDepth(TreeNode* pRoot);
bool IsBalanced_Solution(TreeNode* pRoot);
bool isbalanced(TreeNode* pRoot, int &depth);
};
ListNode* Solution::FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2) {
if (!pHead1 || !pHead2) return nullptr;
int len1 = 0, len2 = 0;
ListNode* pNode1 = pHead1;
ListNode *pNode2 = pHead2;
while (pNode1) {
pNode1 = pNode1->next;
len1++;
}
while (pNode2) {
pNode2 = pNode2->next;
len2++;
}
int deltalen = len2 - len1;
pNode1 = pHead1, pNode2 = pHead2;
int i = 0;
if (deltalen < 0) {
while (i < -deltalen) {
pNode1 = pNode1->next;
i++;
}
}
else
while (i < deltalen) {
pNode2 = pNode2->next;
i++;
}
while (pNode1) {
if (pNode1 == pNode2)
return pNode1;
pNode1 = pNode1->next;
pNode2 = pNode2->next;
}
return nullptr;
}
int Solution::GetNumberOfK(vector<int> data, int k) {
int len = data.size();
if (len == 0 || k < data[0] || data[len - 1] < k) return 0;
int first = firstofk(data, k, 0, len - 1);
int last = lastofk(data, k, 0, len - 1);
if (first == -1 || last == -1) return 0;
return (last - first + 1);
}
int Solution::firstofk(vector<int> data, int k, int begin, int end) {
if (end < begin) return -1;
if (begin == end && data[begin] == k) return begin;
int mid = (begin + end) >> 1;
int result = -1;
if (data[mid] == k) {
if (mid == 0 || data[mid - 1] < k) result = mid;
else
{
result = firstofk(data, k, begin, mid - 1);
}
}
else if (data[mid] > k) result = firstofk(data, k, begin, mid - 1);
else
{
result = firstofk(data, k, mid + 1, end);
}
return result;
}
int Solution::lastofk(vector<int> data, int k, int begin, int end) {
if (end < begin) return -1;
if (begin == end && data[begin] == k) return begin;
int mid = (begin + end) >> 1;
int result = -1;
if (data[mid] == k) {
if (mid == end || data[mid + 1] > k) result = mid;
else
{
result = lastofk(data, k, mid+1, end);
}
}
else if (data[mid] > k) result = lastofk(data, k, begin, mid - 1);
else
{
result = lastofk(data, k, mid + 1, end);
}
return result;
}
int Solution::TreeDepth(TreeNode* pRoot) {
if (!pRoot) return 0;
int depth = 0;
depth = max(TreeDepth(pRoot->left), TreeDepth(pRoot->right))+1;
return depth;
}
bool Solution::IsBalanced_Solution(TreeNode* pRoot) {
int depth = 0;
return isbalanced(pRoot, depth);
}
bool Solution::isbalanced(TreeNode* pRoot, int &depth) {
if (!pRoot) {
depth = 0;
return true;
}
int left, right;
if (isbalanced(pRoot->left, left) && isbalanced(pRoot->right, right)) {
int diff = left - right;
if (diff <= 1 && diff >= -1) {
depth = 1 + (left > right ? left : right);
return true;
}
}
return false;
}
void test1() {
ListNode* pHead1 = new ListNode(1);
pHead1->next = new ListNode(2);
pHead1->next->next = new ListNode(3);
ListNode* p = new ListNode(4);
pHead1->next->next->next = p;
p->next = new ListNode(5);
p->next->next = new ListNode(6);
ListNode* pHead2 = new ListNode(7);
pHead2->next = p;
Solution s;
cout << (s.FindFirstCommonNode(pHead1, pHead2))->val;
return;
}
void test2() {
vector<int> temp = { 1,3,3,3,3,4,5 };
int k = 2;
Solution s;
cout << s.GetNumberOfK(temp, k);
return;
}
void test3() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
Solution s;
cout << s.TreeDepth(root);
return;
}
int main() {
test3();
system("pause");
return 0;
}