文章目录
前言
一个本硕双非的小菜鸡,备战24年秋招,计划刷完卡子哥的刷题计划,加油!
推荐一手卡子哥的刷题网站,感谢卡子哥。代码随想录
一、144. 二叉树的前序遍历
144. 二叉树的前序遍历
Note:中左右
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void travelsal (TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
vec.push_back(cur->val);
travelsal(cur->left, vec);
travelsal(cur->right, vec);
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
travelsal(root, result);
return result;
}
};
二、145. 二叉树的后序遍历
Note:左右中
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void tranversal(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
tranversal(cur->left, vec);
tranversal(cur->right, vec);
vec.push_back(cur->val);
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> result;
tranversal(root, result);
return result;
}
};
三、94. 二叉树的中序遍历
Note:左中右
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void traversal(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
traversal(cur->left, vec);
vec.push_back(cur->val);
traversal(cur->right, vec);
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
traversal(root, result);
return result;
}
};
四、367. 有效的完全平方数
Time / Space:时间4 ms击败25.44%,内存5.8 MB击败31.10%
Note:两个middle相乘会超过int,记着用long
class Solution {
public:
bool isPerfectSquare(int num) {
int leftIndex = 0;
int rightIndex = num;
while (leftIndex <= rightIndex) {
long middle = leftIndex + ((rightIndex - leftIndex) / 2);
if (middle * middle > num) {
rightIndex = middle - 1;
} else if (middle * middle < num) {
leftIndex = middle + 1;
} else {
return true;
}
}
return false;
}
};
五、102. 二叉树的层序遍历
Note:层序遍历又称广度优先搜索,一层层的将数据打印出来,使用的是队列操作。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
vector<vector<int>> result;
while (!que.empty()) {
int size = que.size();
vector<int> vec;
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
vec.push_back(node->val);
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
result.push_back(vec);
}
return result;
}
};
六、226. 翻转二叉树
Note:递归法,先翻转左右节点,在通过前序遍历操作
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
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;
}
};
或者:
Note:使用迭代法来代替递归
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == NULL) return root;
stack<TreeNode*> st;
st.push(root);
while(!st.empty()) {
TreeNode* node = st.top();
st.pop();
swap(node->left, node->right);
if (node->left) st.push(node->left);
if (node->right) st.push(node->right);
}
return root;
}
};
七、101. 对称二叉树
Note:左边是左右中判断,右边是右左中判断,递归
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool compare(TreeNode* left, TreeNode* right) {
if (left == NULL && right != NULL) return false;
else if (left != NULL && right == NULL) return false;
else if (left == NULL && right == NULL) return true;
else if (left->val != right->val) return false;
else {
bool outside = compare(left->left, right->right);
bool inside = compare(left->right, right->left);
bool isSame = outside && inside;
return isSame;
}
}
bool isSymmetric(TreeNode* root) {
if (root == NULL) return true;
return compare(root->left, root->right);
}
};
八、100. 相同的树
Note:递归,每次左或右比较
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if (p != NULL && q == NULL) return false;
else if (p == NULL && q != NULL) return false;
else if (p == NULL && q == NULL) return true;
else if (p->val != q->val) return false;
bool leftIndex = isSameTree(p->left, q->left);
bool rightIndex = isSameTree(p->right, q->right);
bool isSame = leftIndex && rightIndex;
return isSame;
}
};
九、572. 另一棵树的子树
Note:单写一个函数吧
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool compare(TreeNode* root, TreeNode* subRoot) {
if (root != NULL && subRoot == NULL) return false;
else if(root == NULL && subRoot != NULL) return false;
else if(root == NULL && subRoot == NULL) return true;
else if (root->val != subRoot->val) return false;
bool leftIndex = compare(root->left, subRoot->left);
bool rightIndex = compare(root->right, subRoot->right);
bool isSame = leftIndex && rightIndex;
return isSame;
}
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
if (root == NULL) return false;
bool compareIndex = compare(root, subRoot);
bool leftIndex = isSubtree(root->left, subRoot);
bool rightIndex = isSubtree(root->right, subRoot);
bool isSame = compareIndex || leftIndex || rightIndex;
return isSame;
}
};
十、104. 二叉树的最大深度
Note:这次用的是迭代法
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int getdepth(TreeNode* node) {
if (node == NULL) return 0;
int leftdepth = getdepth(node->left);
int rightdepth = getdepth(node->right);
int depth = 1 + max(leftdepth, rightdepth);
return depth;
}
int maxDepth(TreeNode* root) {
return getdepth(root);
}
};
总结
因为二叉树题比较多,就分上中下三篇记录了,系统的搞懂了前中后的两种遍历方式,下面就是广度优先遍历了,二叉树是很多算法的基础,非常重要!