class Solution {
public:
vector<int> arr;
vector<int> preorderTraversal(TreeNode* root) {
if(root==nullptr)
{
return arr;
}
arr.push_back(root->val);
preorderTraversal(root->left);
preorderTraversal(root->right);
return arr;
}
};
2. 二叉树中序遍历
class Solution {
public:
vector<int> arr;
vector<int> inorderTraversal(TreeNode* root) {
if(root==nullptr)
{
return arr;
}
inorderTraversal(root->left);
arr.push_back(root->val);
inorderTraversal(root->right);
return arr;
}
};
3. 二叉树的后序遍历
class Solution {
public:
vector<int> arr;
vector<int> postorderTraversal(TreeNode* root) {
if(root==nullptr)
{
return arr;
}
postorderTraversal(root->left);
postorderTraversal(root->right);
arr.push_back(root->val);
return arr;
}
};
4. 检查两颗树是否相同
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(p==nullptr&&q==nullptr)
return true;
if(p!=nullptr&&q!=nullptr&&p->val==q->val)
return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
return false;
}
};
5. 另一颗树的子树
class Solution {
public:
bool isSametree(struct TreeNode* s, struct TreeNode* t)
{
if (s == NULL && t == NULL)
return true;
if(s!=nullptr&&t!=nullptr&&s->val==t->val)
return isSametree(s->left, t->left) && isSametree(s->right, t->right);
return false;
}
bool isSubtree(struct TreeNode* s, struct TreeNode* t) {
if (s == NULL && t == NULL) return true;
if (s == NULL && t != NULL) return false;
return isSametree(s, t)
|| isSubtree(s->left, t)
|| isSubtree(s->right, t);
}
};
6. 二叉树最大深度
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root==nullptr)
return 0;
int left=maxDepth(root->left);
int right=maxDepth(root->right);
return left>right?left+1:right+1;
}
};
class Solution {
public:
int _bf;
int _Height(TreeNode* root)
{
if (nullptr == root)
return 0;
int leftHeight = _Height(root->left);
int rightHeight = _Height(root->right);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
bool isBalanced(TreeNode* root) {
if (nullptr == root)
return true;
int leftHeight = _Height(root->left);
int rightHeight = _Height(root->right);
_bf=rightHeight-leftHeight;
if (_bf < -1 || _bf > 1)
return false;
return isBalanced(root->left) && isBalanced(root->right);
}
};
8. 对称二叉树
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (root==nullptr)
return true;
queue<TreeNode*> q;
q.push(root->left);
q.push(root->right);
while(!q.empty()) {
TreeNode* lnode = q.front(); q.pop();
TreeNode* rbode = q.front(); q.pop();
if (lnode==nullptr||rbode==nullptr) {
if ( lnode==rbode )
continue;
else
return false;
}
if ( lnode->val != rbode->val )
return false;
q.push( lnode->left );
q.push( rbode->right );
q.push( rbode->left );
q.push( lnode->right );
}
return true;
}
};
9. 二叉树的构建及遍历
#include<iostream>
#include<string>
using namespace std;
struct BTNode{
BTNode *left;
BTNode *right;
char val;
BTNode(char c) :val(c), left(nullptr), right(nullptr) {}
};
string str;
int i;
BTNode* createBTNode()
{
char c = str[i++];
if (c == '#')
return nullptr;
BTNode *root = new BTNode(c);
root->left = createBTNode();
root->right = createBTNode();
return root;
}
void inOrderTraversal(BTNode* root) {
if (root==nullptr)
return;
inOrderTraversal(root->left);
cout << root->val << " ";
inOrderTraversal(root->right);
}
int main()
{
string s;
while(cin>>s)
{
str=s;
i=0;
BTNode *root = createBTNode();
inOrderTraversal(root);
cout << endl;
}
}