- Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
Note: The length of path between two nodes is represented by the number of edges between them.
求树的深度height时,分别遍历左右子树,得到左右子树深度lh和rh,返回左右子树最大深度+1,但同时要跟现已知最大diameter比较,及时更新diameter: diameter为当前节点左右深度之和。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int diameterOfBinaryTree(TreeNode* root) {
int dia = 0;
height(root,dia);
return dia;
}
int height(TreeNode* node, int &diame)
{
if(node == NULL)
return 0;
int lh = 0,rh = 0;
lh = height(node->left,diame);
rh = height(node->right,diame);
int span = lh + rh;
diame = max(span,diame);
return 1 + max(lh,rh);
}
};
- Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.
看清题目是二叉搜索树,因此用中序遍历,传值sum作为一个累积量要不断更新,用sum更新节点值。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* convertBST(TreeNode* root) {
int sum = 0;
inorder(root,sum);
return root;
}
void inorder(TreeNode* node, int &sum) //中序遍历二叉搜索树
{
if(!node)
return;
inorder(node->right,sum);
int tempsum = node->val;
sum += tempsum;
node->val = sum;
inorder(node->left,sum);
}
};
/***** 广度优先 BFS queue*****************/
/**Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
The distance between two adjacent cells is 1.*/
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
//initilize
int m = matrix.size();
int n = matrix[0].size();
int infinity = INT_MAX/2;
vector<vector<int>> newmatrix(m,vector<int>(n,infinity));
queue<pair<int,int>*> que;
int i,j;
for(i = 0;i < m;i++)
for(j = 0;j < n;j++)
{
if(!matrix[i][j])
{
newmatrix[i][j] = 0;
que.push(new pair<int,int>(i,j));
}
}
// que.push(nullptr);
// int dist = 0;
while(!que.empty())
{
pair<int,int>* p = que.front();
que.pop();
// if(p == nullptr)
// {
// dist++; //记录BFS层数
// if(!que.empty())
// que.push(nullptr);
// continue;
// }
i = p->first;
j = p->second;
if(i>0)
{
if(newmatrix[i-1][j] == infinity)
{
newmatrix[i-1][j] = min(newmatrix[i-1][j],newmatrix[i][j]+1);
que.push(new pair<int,int>(i-1,j));
}
}
if(i<m-1)
{
if(newmatrix[i+1][j] == infinity)
{
newmatrix[i+1][j] = min(newmatrix[i+1][j],newmatrix[i][j]+1);
que.push(new pair<int,int>(i+1,j));
}
}
if(j>0)
{
if(newmatrix[i][j-1] == infinity)
{
newmatrix[i][j-1] = min(newmatrix[i][j-1],newmatrix[i][j]+1);
que.push(new pair<int,int>(i,j-1));
}
}
if(j<n-1)
{
if(newmatrix[i][j+1] == infinity)
{
newmatrix[i][j+1] = min(newmatrix[i][j+1],newmatrix[i][j]+1);
que.push(new pair<int,int>(i,j+1));
}
}
delete p;
}
return newmatrix;
}
};