二叉树创建字符串
class Solution {
public:
// 这道题我的思路是 看题: 如果是 左右子树都存在的话,自然是都是有括号(字符)(字符)
// 但:如果左节点不存在: 就是 ()( 字符 )
// 右节点不存在,就是 ( 字符 )
string tree2str(TreeNode* root) {
if(root==nullptr) return "";
string s= to_string(root->val);
if(root->left==nullptr && root->right==nullptr) return s;
if(root->left==nullptr)
{
s+= "()(" + tree2str(root->right)+ ")";
}
else if(root->right==nullptr)
{
s+= "(" + tree2str(root->left)+ ")";
}
else
s+="(" + tree2str(root->left)+ ")(" + tree2str(root->right)+ ")";
return s;
}
};
/*
*大佬的递归操作
*通过题目意思,知道需要在二叉树的前序遍历得到的序列中加入(和)生成字符串。
*题目的生成规则为根(左)(右),当左子树和右子树都为空的情况下为根()(),可以省略不必要的写成根,当只*存在其中一个的时候,左子树存在而右子树不存在根(左),而存在右子树不存在左子树时需要写成根()(右)来*显式表达出是右子树
//作者:hxf_wyh
//链接:https://leetcode-cn.com/problems/construct-string-from-binary-tree/solution/by-
// hxf_wyh-estf/
*/
class Solution {
public:
string res;
void dfs(TreeNode* root) {
if(!root) return;
res += to_string(root->val);
if(root->left || root->right) {
res += "(";
dfs(root->left);
res += ")";
if(root->right) {
res += "(";
dfs(root->right);
res += ")";
}
}
}
string tree2str(TreeNode* root) {
dfs(root);
return res;
}
};
老子和树杠上了!
前序遍历
前序遍历的操作是:中->左->右
/**
* 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<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;
}
};
中序遍历
/**
* 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<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();
st.pop();
result.push_back(cur->val);//中
cur=cur->right;//右
}
}
return result;
}
};
后序遍历
/**
* 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<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;
}
};