前序遍历,中序遍历与后序遍历(迭代法)
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> result;
if (root == NULL) return result;
st.push(root);
while (!st.empty()) {
TreeNode* node = st.top(); // 中
st.pop();
result.push_back(node->val);
if (node->right) st.push(node->right); // 右(空节点不入栈)
if (node->left) st.push(node->left); // 左(空节点不入栈)
}
return result;
}
};
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> st;
TreeNode* cur = root;
while (cur != NULL || !st.empty()) {
if (cur != NULL) { // 指针来访问节点,访问到最底层
st.push(cur); // 将访问的节点放进栈
cur = cur->left; // 左
} else {
cur = st.top(); // 从栈里弹出的数据,就是要处理的数据(放进result数组里的数据)
st.pop();
result.push_back(cur->val); // 中
cur = cur->right; // 右
}
}
return result;
}
};
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> result;
if (root == NULL) return result;
st.push(root);
while (!st.empty()) {
TreeNode* node = st.top();
st.pop();
result.push_back(node->val);
if (node->left) st.push(node->left); // 相对于前序遍历,这更改一下入栈顺序 (空节点不入栈)
if (node->right) st.push(node->right); // 空节点不入栈
}
reverse(result.begin(), result.end()); // 将结果反转之后就是左右中的顺序了
return result;
}
};
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;
// 这里一定要使用固定大小size,不要使用que.size(),因为que.size是不断变化的
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);//注意这里必须是node而不能是root,不然死循环
if (node->right) que.push(node->right);
}
result.push_back(vec);
}
return result;
}
};
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
//递归可以看作先执行后面的
//需要用到返回的指针
if(!root)return nullptr;
TreeNode*treeleft=invertTree(root->left);//记录左
TreeNode*treeright=invertTree(root->right);//记录右
root->left=treeright;//交换
root->right=treeleft;
return root;
}
};
class Solution {
public:
bool judge(TreeNode*root1,TreeNode*root2){
//如果两边有一方为空一方不为空
if((!root1&&root2)||(root1&&!root2)){
return false;
}
else if(root1==nullptr&&root2==nullptr){
return true;
}
//相等的话递归判断后续节点,相当于从后往前传递信息
if(root1->val==root2->val){
return judge(root1->left,root2->right)&&judge(root1->right,root2->left);
}
return false;
}
bool isSymmetric(TreeNode* root) {
if(!root){
return true;
}
else{
return judge(root->left,root->right);
}
}
};
class solution {
public:
int maxdepth(treenode* root) {
if (root == null) return 0;
return 1 + max(maxdepth(root->left), maxdepth(root->right));
}
};
class Solution {
public:
int maxDepth(TreeNode* root) {
//广度优先队列
if(!root)return 0;
int res=0;
queue<TreeNode*>que;
que.push(root);
while(!que.empty()){
res++;//res在这里++,一层一层加
int size=que.size();
for(int i=0;i<size;i++){
TreeNode*point=que.front();
que.pop();
if(point->left)que.push(point->left);
if(point->right)que.push(point->right);
}
}
return res;
}
};
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == NULL) return 0;
if (root->left == NULL && root->right != NULL) {
return 1 + minDepth(root->right);
}
if (root->left != NULL && root->right == NULL) {
return 1 + minDepth(root->left);
}
return 1 + min(minDepth(root->left), minDepth(root->right));
}
};
class Solution {
public:
int minDepth(TreeNode* root) {
if(!root)return 0;
queue<TreeNode*>que;
//导入根节点
que.push(root);
int res=0;
while(que.size()!=0){
res++;
int size=que.size();
for(int i=0;i<size;i++){
TreeNode*point=que.front();
que.pop();
if(point->left)que.push(point->left);
if(point->right)que.push(point->right);
//如果左右节点都不存在了
if(!point->left&&!point->right)return res;
}
}
return res;
}
};
class Solution {
public:
int countNodes(TreeNode* root) {
queue<TreeNode*>que;
if(!root)return 0;
que.push(root);
int res=0;
while(!que.empty()){
res++;
TreeNode*point=que.front();
que.pop();
if(point->left)que.push(point->left);
if(point->right)que.push(point->right);
}
return res;
}
};
class Solution {
public:
int deep(TreeNode*root){
if(!root)return 0;
else return max(deep(root->left),deep(root->right))+1;
}
bool isBalanced(TreeNode* root) {
if(root==nullptr)return true;
if(abs(deep(root->left)-deep(root->right))<=1){
return isBalanced(root->left)&&isBalanced(root->right);
}
return false;
}
};
class Solution {
public:
// 返回以该节点为根节点的二叉树的高度,如果不是平衡二叉树了则返回-1
int getHeight(TreeNode* node) {
if (node == NULL) {
return 0;
}
int leftHeight = getHeight(node->left);
if (leftHeight == -1) return -1;
int rightHeight = getHeight(node->right);
if (rightHeight == -1) return -1;
return abs(leftHeight - rightHeight) > 1 ? -1 : 1 + max(leftHeight, rightHeight);
}
bool isBalanced(TreeNode* root) {
return getHeight(root) == -1 ? false : true;
}
};
class Solution {
public:
vector<int>res;
vector<vector<int>>result;
void search(TreeNode*root){
//回溯
if(!root)return;//如果root为空,直接返回
//不为空的话就导入
res.push_back(root->val);
//终止条件为到叶子节点
if(!root->left&&!root->right){
result.push_back(res);//传递
res.pop_back();//回溯,因为这里就return了,不会进行下面的回溯
return;
}
//正常深度优先搜索+回溯
search(root->left);
search(root->right);
res.pop_back();
}
vector<string> binaryTreePaths(TreeNode* root) {
search(root);
string str;
vector<string>strres;
for(int i=0;i<result.size();i++){
str.clear();
str=to_string(result[i][0]);
for(int j=1;j<result[i].size();j++){
str+="->"+to_string(result[i][j]);
}
strres.push_back(str);
}
return strres;
}
};
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
queue<TreeNode*>que;//广度优先遍历
if(!root)return 0;
que.push(root);
int res=0;
while(que.size()!=0){
TreeNode*point=que.front();
que.pop();
if(point->left){//不能写在一起,不然左节点即使存在也有可能加不进来
//如果这个节点的左节点是左叶子
if(point->left->left==nullptr&&point->left->right==nullptr){
que.push(point->left);
res+=point->left->val;
}
else{
que.push(point->left);
}
}
if(point->right){
que.push(point->right);
}
}
return res;
}
};
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
//广度优先搜索
if(!root)return 0;
queue<TreeNode*>que;
que.push(root);
int result=0;
while(que.size()!=0){
int size=que.size();
for(int i=0;i<size;i++){
TreeNode*point=que.front();
que.pop();
if (i == 0) result = point->val; // 记录每一行第一个元素
//当结束时就自然是最后一行的第一个元素
if(point->left){
que.push(point->left);
}
if(point->right)que.push(point->right);
}
}
return result;
}
};
class Solution {
public:
bool hasPathSum(TreeNode* root, int targetSum) {
if(!root)return false;
//一个条件返回上来
if(targetSum-root->val==0&&root->left==nullptr&&root->right==nullptr){
return true;
}
else{
return hasPathSum(root->left,targetSum-root->val)||hasPathSum(root->right,targetSum-root->val);
}
}
};
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if(inorder.size()==0||postorder.size()==0)return nullptr;
int i=0;
int temp=postorder[postorder.size()-1];
for(i=0;i<postorder.size();i++){
if(inorder[i]==temp){
break;
}
}
//分块在构建
vector<int>inorder1(inorder.begin(),inorder.begin()+i);
vector<int>inorder2(inorder.begin()+i+1,inorder.end());
vector<int>postorder1(postorder.begin(),postorder.begin()+i);
vector<int>postorder2(postorder.begin()+i,postorder.end()-1);
TreeNode*point=new TreeNode(temp);//新建节点
point->left=buildTree(inorder1,postorder1);//递归构建左子树
point->right=buildTree(inorder2,postorder2);//递归构建右子树
return point;//返回
}
};
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if(inorder.size()==0||preorder.size()==0)return nullptr;
int i=0;
int temp=preorder[0];
for(i=0;i<inorder.size();i++){
if(inorder[i]==temp){
break;
}
}
//分块在构建
vector<int>preorder1(preorder.begin()+1,preorder.begin()+i+1);
vector<int>preorder2(preorder.begin()+i+1,preorder.end());
vector<int>inorder1(inorder.begin(),inorder.begin()+i);
vector<int>inorder2(inorder.begin()+i+1,inorder.end());
TreeNode*point=new TreeNode(temp);
point->left=buildTree(preorder1,inorder1);
point->right=buildTree(preorder2,inorder2);
return point;
}
};
class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
if(nums.empty())return nullptr;
int i=0;
int max=nums[0];int j=0;
for(i=0;i<nums.size();i++){
if(nums[i]>max){
max=nums[i];
j=i;
}
}
vector<int>nums1(nums.begin(),nums.begin()+j);
vector<int>nums2(nums.begin()+j+1,nums.end());
TreeNode*point=new TreeNode(max);
point->left=constructMaximumBinaryTree(nums1);
point->right=constructMaximumBinaryTree(nums2);
return point;
}
};
class Solution {
public:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if(root1==nullptr&&root2==nullptr)return nullptr;
int temp=0;
if(root1)temp+=root1->val;
if(root2)temp+=root2->val;
TreeNode*point=new TreeNode(temp);
//分情况
if(root1==nullptr&&root2){
point->left=mergeTrees(nullptr,root2->left);
point->right=mergeTrees(nullptr,root2->right);
}
else if(root2==nullptr&&root1){
point->left=mergeTrees(root1->left,nullptr);
point->right=mergeTrees(root1->right,nullptr);
}
else{
point->left=mergeTrees(root1->left,root2->left);
point->right=mergeTrees(root1->right,root2->right);
}
return point;
}
};
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(root==nullptr)return nullptr;
//小了,去右边找
if(root->val<val){
return searchBST(root->right,val);
}
//大了,去左边找
else if(root->val>val){
return searchBST(root->left,val);
}
//相等就返回
else{
return root;
}
}
};
class Solution {
public:
vector<int>temp;
void search(TreeNode*root){
if(!root)return;
search(root->left);
temp.push_back(root->val);
search(root->right);
}
bool isValidBST(TreeNode* root) {
search(root);
for(int i=0;i<temp.size()-1;i++){
if(temp[i]>=temp[i+1]){
return false;
}
}
return true;
}
};
class Solution {
public:
vector<int>temp;
void search(TreeNode*root){
if(!root)return;
search(root->left);
temp.push_back(root->val);
search(root->right);
}
int getMinimumDifference(TreeNode* root) {
search(root);
int res=temp[1]-temp[0];
vector<int>dp(temp.size()-1,temp[1]-temp[0]);
for(int i=0;i<temp.size()-1;i++){
dp[i]=temp[i+1]-temp[i];
res=min(res,dp[i]);
}
return res;
}
};
class Solution {
public:
vector<int>temp;
void search(TreeNode*root){
if(!root)return;
search(root->left);
temp.push_back(root->val);
search(root->right);
}
vector<int> findMode(TreeNode* root) {
search(root);
unordered_map<int,int>hash;
for(int i=0;i<temp.size();i++){
hash[temp[i]]++;
}
vector<int>res;
int max=0;
for(auto it=hash.begin();it!=hash.end();it++){
if(it->second>max){
max=it->second;
res.clear();
res.push_back(it->first);
}
else if(it->second==max){
res.push_back(it->first);
}
}
return res;
}
};
class Solution {
public:
vector<vector<TreeNode*>>vec;//用二维容器存储
vector<TreeNode*>temp;//回溯
void search(TreeNode*root,TreeNode*point){
if(!root)return;
//不为空则导入
temp.push_back(root);
if(root==point){
vec.push_back(temp);
//这里如果加了return,则不会进行关于root节点的深度优先搜索了
//因此加return的话,需要在这里pop——back,即回溯
}
search(root->left,point);
search(root->right,point);
temp.pop_back();
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
search(root,p);
search(root,q);
int size=min(vec[0].size(),vec[1].size());
int i=0;
for(i=0;i<size;i++){//第一个不同的,或者走到一个数组没了
if(vec[0][i]!=vec[1][i]){
break;
}
}
//返回前一个
return vec[0][i-1];
}
};
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root->val > p->val && root->val > q->val) {
return lowestCommonAncestor(root->left, p, q);
} else if (root->val < p->val && root->val < q->val) {
return lowestCommonAncestor(root->right, p, q);
} else return root;
}
};
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
//返回指针,用后面递归传来的指针进行操作
//root为空,代表此时需要新增节点,并返回
if(!root){
TreeNode*point=new TreeNode(val);
return point;
}
//大了的话,用左递归传来的节点接到左子树上,如果root->left为空的话
//则会返回新节点
if(root->val>val){
root->left=insertIntoBST(root->left,val);
return root;
}
else if(root->val<val){
root->right=insertIntoBST(root->right,val);
return root;
}
return root;
}
};
class Solution {
private:
TreeNode* parent;
void traversal(TreeNode* cur, int val) {
if (cur == NULL) {
TreeNode* node = new TreeNode(val);
if (val > parent->val) parent->right = node;
else parent->left = node;
return;
}
parent = cur;
if (cur->val > val) traversal(cur->left, val);
if (cur->val < val) traversal(cur->right, val);
return;
}
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
parent = new TreeNode(0);
if (root == NULL) {
root = new TreeNode(val);
}
traversal(root, val);
return root;
}
};
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
//依旧返回指针,给上一步用
if(!root)return nullptr;
//大了的话,更新左子树
if(root->val>key)root->left=deleteNode(root->left,key);
else if(root->val<key)root->right=deleteNode(root->right,key);
//相等的话,找到了删除节点,对其更正后返回更正了的节点
else{
if(root->left==nullptr&&root->right){
return root->right;
}
else if(root->right==nullptr&&root->left){
return root->left;
}
else if(root->right==nullptr&&root->left==nullptr){
return nullptr;
}
else{
TreeNode*cur=root->right;
while(cur->left){
cur=cur->left;
}
cur->left = root->left;
root = root->right;
return root;
}
}
return root;
}
};
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int low, int high) {
if(root==nullptr)return nullptr;
//该修剪了
if(root->val>high){
//返回左边的修剪后的节点,递归的传上来
TreeNode*point=trimBST(root->left,low,high);
return point;
}
else if(root->val<low){
TreeNode*point=trimBST(root->right,low,high);
return point;
}
//重接
root->left=trimBST(root->left,low,high);
root->right=trimBST(root->right,low,high);
return root;
}
};
class Solution {
private:
TreeNode* traversal(vector<int>& nums, int left, int right) {
if (left > right) return nullptr;
int mid = left + ((right - left) / 2);
TreeNode* root = new TreeNode(nums[mid]);
root->left = traversal(nums, left, mid - 1);
root->right = traversal(nums, mid + 1, right);
return root;
}
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
TreeNode* root = traversal(nums, 0, nums.size() - 1);
return root;
}
};
class Solution {
private:
int pre = 0; // 记录前一个节点的数值
void traversal(TreeNode* cur) { // 右中左遍历
if (cur == NULL) return;
traversal(cur->right);
cur->val += pre;
pre = cur->val;
traversal(cur->left);
}
public:
TreeNode* convertBST(TreeNode* root) {
pre = 0;
traversal(root);
return root;
}
};