代码随想录:二叉树_01遍历

map、set、multimap、multiset容器底层实现都是平衡二叉搜索树
unordered_set、unordered_map是哈希表

144. 二叉树的前序遍历

前序递归遍历

/**
 * 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:
    void preOrder(TreeNode* node,vector<int>& vec){
        if(node==NULL)
            return;
        vec.push_back(node->val);
        preOrder(node->left,vec);
        preOrder(node->right,vec);
    }
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> vec;
        preOrder(root,vec);
        return vec;
    }
};

前序非递归遍历

法一:

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> stack;
        while(root || !stack.empty()){
            if(root){
                res.push_back(root->val);
                stack.push(root);
                root=root->left;
            }else{
                root=stack.top();
                stack.pop();
                root=root->right;
            }
        }
        return res;
    }
};

法二:

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> stack;
        if(root){
            stack.push(root);
            while(!stack.empty()){
                root=stack.top();
                stack.pop();
                res.push_back(root->val);
                if(root->right)
                    stack.push(root->right);
                if(root->left)
                    stack.push(root->left);
            }
        }
        return res;
    }
};

94. 二叉树的中序遍历

中序递归遍历

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

中序非递归遍历

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> stack;
        while(root || !stack.empty()){
            if(root){
                stack.push(root);
                root=root->left;
            }else{
                root=stack.top();
                stack.pop();
                res.push_back(root->val);
                root=root->right;
            }
        }
        return res;
    }
};

145. 二叉树的后序遍历

后序递归遍历

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

后序非递归遍历

法一:前序非递归法一左右顺序交换再逆转

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> stack;
        while(root || !stack.empty()){
            if(root){
                res.push_back(root->val);
                stack.push(root);
                root=root->right;
            }else{
                root=stack.top();
                stack.pop();
                root=root->left;
            }
        }
        reverse(res.begin(),res.end());
        return res;
    }
};

法二:前序非递归法二左右顺序交换再逆转

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> stack;
        if(root){
            stack.push(root);
            while(!stack.empty()){
                root=stack.top();
                stack.pop();
                res.push_back(root->val);
                if(root->left)
                    stack.push(root->left);
                if(root->right)
                    stack.push(root->right);
            }
        }
        reverse(res.begin(),res.end());
        return res;
    }
};

法三:

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> stack;
        TreeNode* tag=NULL;//标记前面
        while(root || !stack.empty()){
            if(root){
                stack.push(root);
                root=root->left;
            }else{
                root=stack.top();
                if(root->right && root->right!=tag){
                    root=root->right;
                }else{
                    root=stack.top();
                    stack.pop();
                    res.push_back(root->val);
                    tag=root;
                    root=NULL;
                }
            }
        }
        return res;
    }
};

二叉树前中后遍历统一迭代

前序

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> result;
        stack<TreeNode*> st;
        if (root != NULL) st.push(root);
        while (!st.empty()) {
            TreeNode* node = st.top();
            if (node != NULL) {
                st.pop();
                if (node->right) st.push(node->right);  // 右
                if (node->left) st.push(node->left);    // 左
                st.push(node);                          // 中
                st.push(NULL);
            } else {
                st.pop();
                node = st.top();
                st.pop();
                result.push_back(node->val);
            }
        }
        return result;
    }
};

中序

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> result;
        stack<TreeNode*> st;
        if (root != NULL) st.push(root);
        while (!st.empty()) {
            TreeNode* node = st.top();
            if (node != NULL) {
                st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
                if (node->right) st.push(node->right);  // 添加右节点(空节点不入栈)

                st.push(node);                          // 添加中节点
                st.push(NULL); // 中节点访问过,但是还没有处理,加入空节点做为标记。

                if (node->left) st.push(node->left);    // 添加左节点(空节点不入栈)
            } else { // 只有遇到空节点的时候,才将下一个节点放进结果集
                st.pop();           // 将空节点弹出
                node = st.top();    // 重新取出栈中元素
                st.pop();
                result.push_back(node->val); // 加入到结果集
            }
        }
        return result;
    }
};

后序

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> result;
        stack<TreeNode*> st;
        if (root != NULL) st.push(root);
        while (!st.empty()) {
            TreeNode* node = st.top();
            if (node != NULL) {
                st.pop();
                st.push(node);                          // 中
                st.push(NULL);

                if (node->right) st.push(node->right);  // 右
                if (node->left) st.push(node->left);    // 左

            } else {
                st.pop();
                node = st.top();
                st.pop();
                result.push_back(node->val);
            }
        }
        return result;
    }
};

102. 二叉树的层序遍历

层序遍历

法一:用队列实现

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> res;
        queue<TreeNode*> que;
        if(root)
            que.push(root);
        int size;
        while(!que.empty()){
            size=que.size();
            vector<int> vec;
            while(size--){
                root=que.front();
                que.pop();
                vec.push_back(root->val);
                if(root->left)
                    que.push(root->left);
                if(root->right)
                    que.push(root->right);
            }
            res.push_back(vec);
        }
        return res;
    }
};

法二:递归

class Solution {
public:
    void order(TreeNode* cur,vector<vector<int>>& res,int depth){
        if(cur==NULL)
            return;
        if(res.size() == depth)
            res.push_back(vector<int>());
        res[depth].push_back(cur->val);
        order(cur->left,res,depth+1);
        order(cur->right,res,depth+1);
    }
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> res;
        int depth=0;
        order(root,res,depth);
        return res;
    }
};

199. 二叉树的右视图

class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        vector<int> res;
        queue<TreeNode*> que;
        if(root)
            que.push(root);
        int size;
        while(!que.empty()){
            size=que.size();
            while(size--){
                root=que.front();
                que.pop();
                if(root->left)
                    que.push(root->left);
                if(root->right)
                    que.push(root->right);
            }
            res.push_back(root->val);
        }
        return res;
    }
};

429. N 叉树的层序遍历

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;
    Node() {}
    Node(int _val) {
        val = _val;
    }
    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
        vector<vector<int>> res;
        queue<Node*> que;
        if(root)
            que.push(root);
        int size;
        while(!que.empty()){
            size=que.size();
            vector<int> vec;
            while(size--){
                root=que.front();
                que.pop();
                vec.push_back(root->val);
                for (Node* child: root->children) {
                    que.push(child);
                }
            }
            res.push_back(vec);
        }
        return res;
    }
};

515. 在每个树行中找最大值

class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        vector<int> res;
        queue<TreeNode*> que;
        if(root)
            que.push(root);
        while(!que.empty()){
            int size=que.size();
            int max = INT32_MIN;//INT32_MIN最小负整数、INT32_MAX最大正整数
            while(size--){
                root=que.front();
                que.pop();
                if(max<root->val)
                    max=root->val;
                if(root->left)
                    que.push(root->left);
                if(root->right)
                    que.push(root->right);
            }
            res.push_back(max);
        }
        return res;
    }
};

116. 填充每个节点的下一个右侧节点指针
117. 填充每个节点的下一个右侧节点指针 II
法一:队列实现,两道题均可实现

class Solution {
public:
    Node* connect(Node* root) {
        queue<Node*> que;
        if(root)
            que.push(root);
        Node* p=root;
        while(!que.empty()){
            int size=que.size();
            while(size--){
                p=que.front();
                que.pop();
                if(size)
                    p->next=que.front();
                if(p->left)
                    que.push(p->left);
                if(p->right)
                    que.push(p->right);
            }
            p->next=NULL;
        }
        return root;
    }
};

法二:递归,只可实现116

class Solution {
public:
    Node* connect(Node* root) {
        if(!root || !root->left || !root->right)
            return root;
        root->left->next = root->right;
        if(root->next)
            root->right->next = root->next->left;
        connect(root->left);
        connect(root->right);
        return root;
    }
};

104. 二叉树的最大深度
法一:队列实现

class Solution {
public:
    int maxDepth(TreeNode* root) {
        int depth=0;
        queue<TreeNode*> que;
        if(root)
            que.push(root);
        while(!que.empty()){
            int size=que.size();
            while(size--){
                root=que.front();
                que.pop();
                if(root->left)
                    que.push(root->left);
                if(root->right)
                    que.push(root->right);
            }
            depth++;
        }
        return depth;
    }
};

法二:递归

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (root == NULL) 
        	return 0;
        return max(maxDepth(root->left), maxDepth(root->right)) + 1;
    }
};

111. 二叉树的最小深度

法一:队列实现

class Solution {
public:
    int minDepth(TreeNode* root) {
        int depth=0;
        queue<TreeNode*> que;
        if(root)
            que.push(root);
        while(!que.empty()){
            depth++;
            int size=que.size();
            while(size--){
                root=que.front();
                que.pop();
                if(root->left==NULL && root->right==NULL){
                    return depth;
                }
                if(root->left)
                    que.push(root->left);
                if(root->right)
                    que.push(root->right);
            }
        }
        return depth;
    }
};

法二:递归

class Solution {
public:
        int minDepth(TreeNode* root) {
        if (!root) return 0;
        if (!root->left) return minDepth(root->right) + 1;
        if (!root->right) return minDepth(root->left)  + 1;
        return min(minDepth(root->left), minDepth(root->right)) + 1;
    }
};
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值