一、二叉树的遍历
1.前序遍历
144题
/**
* 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 {
private:
void recursion(TreeNode *root, vector<int> &ans) {
if (!root) return;
ans.push_back(root->val);
recursion(root->left, ans);
recursion(root->right, ans);
}
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> ans;
recursion(root, ans);
return ans;
}
};
2.中序遍历
/**
* 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 {
private:
void recursion(TreeNode* root, vector<int> &ans) {
if (!root) return;
recursion(root->left, ans);
ans.push_back(root->val);
recursion(root->right, ans);
}
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> ans;
recursion(root, ans);
return ans;
}
};
3.后序遍历
145题
/**
* 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 {
private:
void recursion(TreeNode* root, vector<int> &ans) {
if (!root) return;
recursion(root->left, ans);
recursion(root->right, ans);
ans.push_back(root->val);
}
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ans;
recursion(root, ans);
return ans;
}
};
以上时间复杂度O(n),平均空间复杂度O(logn),最坏空间复杂度为O(n)
4. 层序遍历
102题
思路:使用队列,每次将首元素的非空左右子树入队,然后首元素出队,这样对每一层结点进行一次循环,最终就可以得到层次遍历的结果?
/**
* 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) {
// 1.准备工作
queue<TreeNode*> q;
q.push(root);
vector<vector<int>> ans;
vector<int> cur_layer;
// 2.正式处理
while (!q.empty()) {
int sz = q.size();
cur_layer.clear();
// 出队并将首元素的非空左右子树压入队列中
for (int i=0; i<sz; ++i) {
TreeNode *head = q.front();
if (head) {
cur_layer.push_back(head->val);
q.push(head->left);
q.push(head->right);
}
q.pop();
}
if (cur_layer.size() > 0) {
ans.push_back(cur_layer);
}
}
return ans;
}
};
每个结点入队出队一次,时间复杂度O(n),空间复杂度最坏O(n)
二、二叉查找树/搜索树相关
1. 二叉树的深度
leetcode 104题
/**
* 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 maxDepth(TreeNode* root) {
if (!root) return 0;
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};
时间复杂度O(n),空间复杂度平均O(height)
2. 验证二叉搜索树
leetcode 98
|