226.翻转二叉树
思路:前序遍历,交换左右结点的值
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == NULL) return root;
swap(root->left, root->right); // 中
invertTree(root->left); // 左
invertTree(root->right); // 右
return root;
}
};
101. 对称二叉树
使用队列,分别把左右结点存入队列判断left->leftright->left , left->rightright->right,
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (root == NULL) return true;
queue<TreeNode*> que;
que.push(root->left); // 将左子树头结点加入队列
que.push(root->right); // 将右子树头结点加入队列
while (!que.empty()) { // 接下来就要判断这这两个树是否相互翻转
TreeNode* leftNode = que.front(); que.pop();
TreeNode* rightNode = que.front(); que.pop();
if (!leftNode && !rightNode) { // 左节点为空、右节点为空,此时说明是对称的
continue;
}
// 左右一个节点不为空,或者都不为空但数值不相同,返回false
if ((!leftNode || !rightNode || (leftNode->val != rightNode->val))) {
return false;
}
que.push(leftNode->left); // 加入左节点左孩子
que.push(rightNode->right); // 加入右节点右孩子
que.push(leftNode->right); // 加入左节点右孩子
que.push(rightNode->left); // 加入右节点左孩子
}
return true;
}
};
104.二叉树的最大深度
层序遍历结点,当结束一层计数器+1
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == NULL) return 0;
int depth = 0;
queue<TreeNode*> que;
que.push(root);
while(!que.empty()) {
int size = que.size();
depth++; // 记录深度
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return depth;
}
};
104.二叉树的最小深度
层序遍历,当某个结点没有左右结点时返回层数
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == NULL) return 0;
int depth = 0;
queue<TreeNode*> que;
que.push(root);
while(!que.empty()) {
int size = que.size();
depth++; // 记录最小深度
int flag = 0;
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
if (!node->left && !node->right) { // 当左右孩子都为空的时候,说明是最低点的一层了,退出
flag = 1;
break;
}
}
if (flag == 1) break;
}
return depth;
}
};
222.完全二叉树的节点个数
层序遍历,如果存在结点计数器增加
class Solution {
public:
int countNodes(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
int result = 0;
while (!que.empty()) {
int size = que.size();
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
result++; // 记录节点数量
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return result;
}
};
*110.平衡二叉树
求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中)
后序遍历
递归:
终止条件:当求高度至叶子结点
单层逻辑:获得左右结点的深度,判断abs(l-r)是否大于1。如果大于1则return -1,否则高度等于1+max(l-r)
class Solution {
public:
// 返回以该节点为根节点的二叉树的高度,如果不是二叉搜索树了则返回-1
int getDepth(TreeNode* node) {
if (node == NULL) {
return 0;
}
int leftDepth = getDepth(node->left);
if (leftDepth == -1) return -1; // 说明左子树已经不是二叉平衡树
int rightDepth = getDepth(node->right);
if (rightDepth == -1) return -1; // 说明右子树已经不是二叉平衡树
return abs(leftDepth - rightDepth) > 1 ? -1 : 1 + max(leftDepth, rightDepth);
}
bool isBalanced(TreeNode* root) {
return getDepth(root) == -1 ? false : true;
}
};
257. 二叉树的所有路径
vector<string> traversal(TreeNode* root)
{
stack<TreeNode*>treeSt;//树结点记录
stack<string>pathSt;//路径记录
vector<string>result;//最终路径集合
if (root == NULL)
{
return result;
}
treeSt.push(root);
pathSt.push(root);
while (!treeSt.empty())
{
TreeNode* node = treeSt.top();
treeSt.pop();
string path = pathSt.top();
pathSt.pop();
if (node->left == NULL && node->right == NULL)
{
result.push_back(path);
}
if (node->right)
{
treeSt.push(node->right);
pathSt.push(path + "->" + to_string(node->right->val));
}
if (node->left)
{
treeSt.push(node->left);
pathSt.push(path + "->" + to_string(node->left->val));
}
}
return result;
100.求相同的树
递归
返回bool
终止条件:根结点不同,一方为空,双方为空。
单层逻辑:判断左节点是否相同,判断右结点是否相同,返回两者布尔的与。
bool compare(TreeNode* tree1, TreeNode* tree2)
{
//终止条件:根结点不一样、左节点不一样、右节点不一样
//1->left=2->left,1->right=2->right
if (tree1 == NULL && tree2 != NULL)return false;
else if (tree1 != NULL && tree2 == NULL)return false;
else if (tree1 == NULL && tree2 == NULL)return true;
else if (tree1->val != tree2->val)return false;
bool t1 = compare(tree1->left, tree2->left);
bool t2 = compare(tree1->right, tree2->right);
return t1&&t2;
}
bool isSameTree(TreeNode* p, TreeNode* q)
{
return compare(p, q);
}