二叉树算法模板

1.二叉树遍历的迭代模板

前序遍历

前序遍历的顺序是根左右,为了防止破坏树结构,我们将root地址给了tmp,开始只要tmp不空,他一定是根节点或左节点,因为在if语句中我们令tmp = tmp->left,因此在if语句中我们把相应的val值给了ans,如果tmp空了,说明左节点遍历完了,我们找到相应的右节点开始遍历:

/**
 * Definition for a binary tree node.
 * 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> ans;
        if(!root)return ans;
        TreeNode *tmp = root,*tmp2;
        stack<TreeNode*> st;
        while(tmp || !st.empty())
        {
            if(tmp)
            {
                st.push(tmp);
                ans.push_back(tmp->val);
                tmp = tmp->left;
            }
            else{
                tmp2 = st.top();
                st.pop();
                tmp = tmp2->right;
            }
        }
        return ans;
    }
};

中序遍历

中序遍历的顺序是左根右,我们只需要把前序遍历的ans.push_back操作从if语句搬到else语句中即可:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> a;
        stack<TreeNode*> st;
        TreeNode* tmp = root;
        while(st.size() || tmp)
        {
            if(tmp){
                st.push(tmp);
                tmp = tmp->left;
            }
            else{
                a.push_back(st.top()->val);
                tmp = st.top();
                st.pop();
                tmp = tmp->right;
            }
        }
        return a;
    }
};

后序遍历

后序遍历的顺序是左右根,我们把入栈的顺序变为根右左(前序遍历入栈顺序根左右),如果我们把入栈顺序倒过来,便变成了左右根,因此我们把前序遍历if中指向左节点改为右节点,val从end加入改为从begin前加入,else中的指向右节点改为指向左节点。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> ans;
        if(!root)return ans;
        stack<TreeNode*> st;
        TreeNode* tmp=root,*tmp2;
        while(tmp || !st.empty())
        {
            if(tmp)
            {
                st.push(tmp);
                ans.insert(ans.begin(),tmp->val);
                tmp = tmp->right;
            }
            else{
                tmp2 = st.top();
                st.pop();
                tmp = tmp2->left;
            }
        }
        return ans;
    }
};

综上,我们有模板如下:

while(tmp || !st.empty())
{
    if(tmp)
    {
        ...
    }
    else{
        ...
    }
}

2.二叉树路径问题的递归模板

这类问题有112. 路径总和,113. 路径总和 II,129. 求根到叶子节点数字之和

if(!root->left && !root->right && condition)
{

}
if(root->left)
{
      
}
if(root->right)
{
           
}  

3.二叉树层次遍历的模板

这类问题有102. 二叉树的层次遍历,103. 二叉树的锯齿形层次遍历,107. 二叉树的层次遍历 II,111. 二叉树的最小深度

queue<TreeNode*> q;
q.push(root);
while(!q.empty())
{
    size = q.size();
    for(int i = 0; i < size; i++)
    {
       tmp = q.front();
       q.pop();
       if(!tmp->left && !tmp->right)
       {

       }
       if(tmp->left)q.push(tmp->left);
       if(tmp->right)q.push(tmp->right);
    }
          
}

版权声明
作者:曲径通霄
出处:博客园曲径通霄的技术博客--https://www.cnblogs.com/qujingtongxiao/
您的支持是对博主最大的鼓励,感谢您的认真阅读。
本文版权归作者所有,如需转载,请联系作者,且必须保留此段声明,
在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
-------------------------------------------------------------------

猜你喜欢

转载自www.cnblogs.com/qujingtongxiao/p/12432795.html
今日推荐