发布时间:2024-02-03 08:30
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int>v;
stack<TreeNode *>s;
if(root==nullptr){
return {};
}
s.push(root);
while(!s.empty()){
TreeNode *temp=s.top();
v.push_back(temp->val);
s.pop();
if(temp->right){
s.push(temp->right);
}
if(temp->left){
s.push(temp->left);
}
}
return v;
}
}
- 遍历顺序:左前右
class Solution{
public:
vector<int>inorderTraversal(TreeNode*root)
{
vector<int>v;
stack<TreeNode *>s;
if(root==nullptr){
return {};
}
TreeNode *cur=root;
//一开始为空栈避免直接退出循环。
while(cur||!s.empty())
{
if(cur){
s.push(cur);
cur=cur->left;
}
else{
cur=s.top();
s.pop();
v.push_back(cur->val);
cur=cur->right;
}
}
return v;
}
};
class Solution{
private:
vector<int>v;
public:
vector<int> postorderTraversal(TreeNode* root)
{
vector<int>v;
stack<TreeNode *>s;
if(root==nullptr)
{
return {};
}
s.push(root);
while(!s.empty())
{
TreeNode *temp=s.top();
s.pop();
v.push_back(temp->val);
if(temp->left){
s.push(temp->left);
}
if(temp->right){
s.push(temp->right);
}
}
reverse(v.begin(),v.end());
return v;
}
};
遍历顺序:一层一层进行遍历
利用队列先进先出
class Solution {
vector<vector<int>>res;
public:
vector<vector<int>> levelOrder(TreeNode* root) {
if(root==nullptr){
return {};
}
queue<TreeNode *>q;
q.push(root);
while(!q.empty()){
vector<int>v;
int size=q.size();
for(int i=0;i<size;i++){
TreeNode *temp=q.front();
v.push_back(temp->val);
q.pop();
if(temp->left){
q.push(temp->left);
}
if(temp->right){
q.push(temp->right);
}
}
res.push_back(v);
}
return res;
}
};
求普通二叉树的属性一般使用后序,通过递归函数的返回值做计算。
if(left&&!right){ return false; } else if(!left&&right){ return false; } else if(!left&&!right){ return true; } else if(left&&right&&left->val!=right->val){ return false; }
class Solution {
public:
bool compare(TreeNode *left,TreeNode *right){
if(left&&!right){
return false;
}
else if(!left&&right){
return false;
}
else if(!left&&!right){
return true;
}
else if(left&&right&&left->val!=right->val){
return false;
}
bool bool_left=compare(left->right,right->left);
bool bool_right=compare(left->left,right->right);
bool is_same=bool_left&&bool_right;
return is_same;
}
bool isSymmetric(TreeNode* root) {
if(root==nullptr){
return true;
}
return compare(root->left,root->right);
}
};
if(root==nullptr){ return 0; }
,遇到空节点返回数值0class Solution {
public:
int maxDepth(TreeNode* root) {
if(root==nullptr){
return 0;
}
int leftdepth=1+maxDepth(root->left);
int rightdepth=1+maxDepth(root->right);
int depth=max(leftdepth,rightdepth);
return depth;
}
class Solution {
public:
int minDepth(TreeNode* root) {
if(root==nullptr){
return 0;
}
int leftdepth=minDepth(root->left);
int rightdepth=minDepth(root->right);
if(root->left&&!root->right){
return 1+leftdepth;
}
if(!root->left&&root->right){
return 1+rightdepth;
}
return 1+min(leftdepth,rightdepth);
}
};
class Solution {
public:
int countNodes(TreeNode* root) {
if(root==nullptr){
return 0;
}
int leftnodes=countNodes(root->left);
int rightnodes=countNodes(root->right);
int node=leftnodes+rightnodes+1;
return node;
}
};
平衡二叉树
class Solution {
public:
int getheight(TreeNode *root){
if(root==nullptr){
return 0;
}
int leftheight=getheight(root->left);
if(leftheight==-1){
return -1;
}
int rightheight=getheight(root->right);
if(rightheight==-1){
return -1;
}
if(abs(leftheight-rightheight)>1){
return -1;
}
else{
return 1+max(leftheight,rightheight);
}
}
bool isBalanced(TreeNode* root) {
return getheight(root)==-1?false:true;
}
};
6.二叉树的所有路径