leetcode解题模板 —— 树的遍历

1.二叉树前序遍历

模板:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
  void order(TreeNode* root) {
  	if (root == NULL) {return;}
  	/*执行动作*/
  	order(root->left);
  	order(root->right);
  }

实战:
leetcode 144. 二叉树的前序遍历

class Solution {
public:
    void order(TreeNode* root, vector<int> &ans){
        if(root == NULL){return;}
        ans.push_back(root->val);
        order(root->left, ans);
        order(root->right, ans);
    }
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> ans;
        order(root, ans);
        return ans;
    }
};

2.二叉树的中序遍历

模板:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
  void order(TreeNode* root) {
  	if (root == NULL) {return;}
  	order(root->left);
  	/*执行动作*/
  	order(root->right);
  }

实战:
leetcode 94. 二叉树的中序遍历

class Solution {
public:
    void order(TreeNode* root, vector<int> &ans){
        if(root == NULL){return;}
        order(root->left, ans);
         ans.push_back(root->val);
        order(root->right, ans);
    }
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> ans;
        order(root, ans);
        return ans;
    }
};

3.二叉树的后序遍历

模板:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
  void order(TreeNode* root) {
  	if (root == NULL) {return;}
  	order(root->left);
  	order(root->right);
  	/*执行动作*/
  }

实战:
leetcode 145. 二叉树的后序遍历

class Solution {
public:
    void order(TreeNode* root, vector<int> &ans){
        if(root == NULL){return;}
        order(root->left, ans);
        ans.push_back(root->val);
        order(root->right, ans);
    }
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> ans;
        order(root, ans);
        return ans;
    }
};

4.二叉树的层序遍历

模板:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 * /
 void levelOrder(TreeNode* root) {
        queue<TreeNode*> q;
        if(root == nullptr){return ;}
        q.push(root);
        while(!q.empty())
        {
            int curSize = q.size();         
            for(int i = 0; i < curSize; i++){
                TreeNode* node = q.front();
                q.pop();
               	/*执行动作*/
                if(node->left != nullptr){q.push(node->left);}
                if(node->right != nullptr){q.push(node->right);}
            }
        }
    }
 

实战:
leetcode 102. 二叉树的层序遍历

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        queue<TreeNode*> q;
        vector<vector<int>> res;
        if(root == nullptr){return res;}
        q.push(root);
        while(!q.empty())
        {
            int curSize = q.size();
            res.push_back(vector<int>());           
            for(int i = 0; i < curSize; i++){
                TreeNode* node = q.front();
                q.pop();
                res.back().push_back(node->val);
                if(node->left != nullptr){q.push(node->left);}
                if(node->right != nullptr){q.push(node->right);}
            }            
        }
        return res;
    }
};

leetcode 107. 二叉树的层次遍历 II

class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> res;
        queue<TreeNode*> q;
        if(root == nullptr){return res;}
        q.push(root);
        while(!q.empty()){
            int curSize = q.size();
            res.push_back(vector<int>());
            for(int i = 0; i < curSize; i++){
                TreeNode* node = q.front();
                q.pop();
                res.back().push_back(node->val);
                if(node->left != nullptr){q.push(node->left);}
                if(node->right != nullptr){q.push(node->right);}
            }
        }
        reverse(res.begin(),res.end());
        return res;
    }
};

leetcode 103. 二叉树的锯齿形层次遍历

class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> res;
        queue<TreeNode*> q;
        if(root == nullptr){return res;}
        q.push(root);
        bool f = false;
        while(!q.empty()){
            int curSize = q.size();
            res.push_back(vector<int>());
            for(int i = 0; i < curSize; i++){
                TreeNode* node = q.front();
                q.pop();
                res.back().push_back(node->val);
                if(node->left != nullptr){q.push(node->left);}
                if(node->right != nullptr){q.push(node->right);}
            }
            if(f){
                reverse(res.back().begin(),res.back().end());
            }
            f =!f;
        }
        return res;
    }
};

5.提升练习
leetcode 988. 从叶结点开始的最小字符串

class Solution {
public:
    void order(TreeNode *root, vector<string> &ansVec, string ans)
    {
        if (root->left == nullptr && root->right == nullptr) {
            ans += root->val + 'a';
            ansVec.push_back(ans);
            return;
        }
        ans += root->val + 'a';
        if (root->left != nullptr) {
            order(root->left, ansVec, ans);
        }
        if (root->right != nullptr) {
            order(root->right, ansVec, ans);
        }
    }
    string smallestFromLeaf(TreeNode *root)
    {
        string ans;
        if (root == nullptr) {
            return ans;
        }
        vector<string> ansVec;
        order(root, ansVec, ans);
        for (int i = 0; i < ansVec.size(); i++) {
            reverse(ansVec[i].begin(), ansVec[i].end());
        }
        sort(ansVec.begin(), ansVec.end());
        return ansVec[0];
    }
};

leetcode解题模板——目录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不放弃的蜗牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值