代码随想录训练营Day10 | 二叉树的遍历

递归遍历

c++ 复制代码
   void preRecur(vector<int>& ans, TreeNode* root) {
        if(root) {
            ans.push_back(root->val);
            preRecur(ans, root->left);
            preRecur(ans, root->right);
        }
    }
  1. 145.二叉树的后序遍历
c++ 复制代码
 void sufRecur(vector<int>& ans, TreeNode* root) {
        if(root) {
            sufRecur(ans, root->left);
            sufRecur(ans, root->right);
            ans.push_back(root->val);
        }
    }
  1. 94.二叉树的中序遍历
c++ 复制代码
 void inRecur(vector<int>& ans, TreeNode* root) {
        if(root) {
            inRecur(ans, root->left);
            ans.push_back(root->val);
            inRecur(ans, root->right);
          
        }
    }

层序遍历

c++ 复制代码
void preIter(vector<int>& ans, TreeNode* root) {
        stack<TreeNode*> s;
        s.push(root);
        while(!s.empty()) {
            TreeNode* t = s.top();
            ans.push_back(t->val);
            s.pop();

            if(t->right)
                s.push(t->right);
            if(t->left)
                s.push(t->left);
        }
    }
  1. 145.二叉树的后序遍历
c++ 复制代码
 void sufIter(vector<int>& ans, TreeNode* root) {
        stack<TreeNode*> s;
        TreeNode* cur = root;
        TreeNode* prev = nullptr;
        while(cur || !s.empty()) {
            while(cur) {
                s.push(cur);
                cur = cur->left;
            }
            cur = s.top();
            s.pop();
            if(cur->right == nullptr || cur->right == prev) {
                ans.push_back(cur->val);
                prev = cur;
                cur = nullptr;
            } else {
                s.push(cur);
                cur = cur->right;
            }
        }
    }
  1. 94.二叉树的中序遍历
c++ 复制代码
  void inIter(vector<int>& ans, TreeNode* root) {
        stack<TreeNode*> s;
        TreeNode* cur = root;
        while(cur || !s.empty()) {
            while(cur) {
                s.push(cur);
                cur = cur->left;
            }

            cur = s.top();
            s.pop();
            ans.push_back(cur->val);
            cur = cur->right;
        }
    }

二叉树的层序遍历

c++ 复制代码
class Solution {
public:
    
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> ans;
        if(!root) return ans;
        TreeNode* cur = nullptr;
        deque<TreeNode*> dq;
        dq.push_back(root); // 双端队列
        while(!dq.empty()) {
            vector<int> t;
            int n = dq.size(); // 遍历一层
            for(int i = 0; i < n; ++i) {
                cur = dq.front();
                dq.pop_front();
                t.push_back(cur->val);
                if(cur->left)
                    dq.push_back(cur->left);
                if(cur->right)
                    dq.push_back(cur->right);
            }
            ans.push_back(t);
        }
        return ans;
    }
};

107.二叉树的层次遍历II

c++ 复制代码
class Solution {
public:
	// 思路与上题一致
    vector<vector<int>> levelOrderBottom(TreeNode *root) {
        if (root == nullptr) return {};
        vector<vector<int>> ans;
        vector<TreeNode*> cur{root};
        while (cur.size()) {
            vector<TreeNode*> nxt;
            vector<int> vals;
            for (auto node : cur) {
                vals.push_back(node->val);
                if (node->left)  nxt.push_back(node->left);
                if (node->right) nxt.push_back(node->right);
            }
            cur = move(nxt);
            ans.emplace_back(vals);
        }
        ranges::reverse(ans);
        return ans;
    }
};

199.二叉树的右视图

c++ 复制代码
class Solution {
    vector<int> ans;

    void dfs(TreeNode* node, int depth) {
        if (node == nullptr) {
            return;
        }
        if (depth == ans.size()) { // 这个深度首次遇到
            ans.push_back(node->val);
        }
        dfs(node->right, depth + 1); // 先递归右子树,保证首次遇到的一定是最右边的节点
        dfs(node->left, depth + 1);
    }

public:
    vector<int> rightSideView(TreeNode* root) {
        dfs(root, 0);
        return ans;
    }
};

637.二叉树的层平均值

c++ 复制代码
class Solution {
public:
    vector<double> averageOfLevels(TreeNode* root) {
        vector<double> ans;
        if(!root) return ans;
        TreeNode* cur = nullptr;
        deque<TreeNode*> dq;
        dq.push_back(root);
        while(!dq.empty()) {
            int n = dq.size();
            double sum = 0;
            for(int i = 0; i < n; ++i) {
                cur = dq.front();
                dq.pop_front();
                sum += cur->val;                
                if(cur->left)
                    dq.push_back(cur->left);
                if(cur->right)
                    dq.push_back(cur->right);
            }
            ans.push_back(sum / n);
        }
        return ans;
    }
};

429.N叉树的层序遍历

c++ 复制代码
class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
         vector<vector<int>> ans;
        if(!root) return ans;
        Node* cur = nullptr;
        deque<Node*> dq;
        dq.push_back(root);
        while(!dq.empty()) {
            vector<int> t;
            int n = dq.size();
            for(int i = 0; i < n; ++i) {
                cur = dq.front();
                dq.pop_front();
                t.push_back(cur->val);
                for(Node* t : cur->children) 
                    dq.push_back(t);
            }
            ans.push_back(t);
        }
        return ans;
    }
};

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

c++ 复制代码
class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        if (!root) {
            return {};
        }
        vector<int> res;
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty()) {
            int len = q.size();
            int maxVal = INT_MIN;
            while (len > 0) {
                len--;
                auto t = q.front();
                q.pop();
                maxVal = max(maxVal, t->val);
                if (t->left)  q.push(t->left);
                if (t->right) q.push(t->right);
            }
            res.push_back(maxVal);
        }
        return res;
    }
};

116.填充每个节点的下一个右侧节点指针

c++ 复制代码
class Solution {
public:
    Node* connect(Node* root) {
        if(!root) return nullptr;
        Node* cur = nullptr;
        deque<Node*> dq;
        dq.push_back(root);
        while(!dq.empty()) {
            int n = dq.size();
            for(int i = 0; i < n; ++i) {
                cur = dq.front();
                dq.pop_front();
                if(i != n - 1)
                    cur->next = dq.front();  // 填充next
                if(cur->left) dq.push_back(cur->left);
                if(cur->right) dq.push_back(cur->right);
            }
        }
        return root;
    }
};

117.填充每个节点的下一个右侧节点指针II

c++ 复制代码
class Solution {
public:
    Node* connect(Node* root) {
        if(!root) return nullptr;
        Node* cur = nullptr;
        deque<Node*> dq;
        dq.push_back(root);
        while(!dq.empty()) {
            int n = dq.size();
            for(int i = 0; i < n; ++i) {
                cur = dq.front();
                dq.pop_front();
                if(i != n - 1)
                    cur->next = dq.front(); // 填充next
                if(cur->left) dq.push_back(cur->left);
                if(cur->right) dq.push_back(cur->right);
            }
        }
        return root;
    }
};

104.二叉树的最大深度

c++ 复制代码
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (root == nullptr) {
            return 0;
        }
        int l_depth = maxDepth(root->left);
        int r_depth = maxDepth(root->right);
        return max(l_depth, r_depth) + 1; // 记录高度,记录高度最大值
    }
};

111.二叉树的最小深度

c++ 复制代码
class Solution {
    int ans = INT_MAX; // 遍历时记录高度,遍历到叶子节点取高度最小值
    void dfs(TreeNode *node, int cnt) {
        if (node == nullptr) {
            return;
        }
        cnt++;
        if (node->left == node->right) { // node 是叶子
            ans = min(ans, cnt);
            return;
        }
        dfs(node->left, cnt);
        dfs(node->right, cnt);
    };
public:
    int minDepth(TreeNode *root) {
        dfs(root, 0);
        return root ? ans : 0;
    }
};
相关推荐
Flower#29 分钟前
【图论】判断图中有环的两种方法及实现
算法·深度优先·图论
胡桃不是夹子38 分钟前
学会了蛇形矩阵
c++·算法·矩阵
m0_675988231 小时前
Leetcode2597:美丽子集的数目
算法·leetcode·回溯·python3
丶重明1 小时前
【Go每日一练】返回切片中的最大值和最小值
算法·golang
一只_程序媛1 小时前
【leetcode hot 100 234】回文链表
算法·leetcode·链表
qq_433554541 小时前
C++ 二叉搜索树代码
开发语言·c++·算法
Coder Zhang2 小时前
后序线索化二叉树,并找到指定结点前驱,非递归逆序输出
数据结构·算法
云边有个稻草人2 小时前
【数据结构初阶第十八节】八大排序系列(上篇)—[详细动态图解+代码解析]
算法·冒泡排序·堆排序·希尔排序·八大排序·快排·直接插入排序
iFulling4 小时前
【数据结构】第六章:图
数据结构·笔记·算法
原来是猿5 小时前
蓝桥备赛(九)- 结构体和类
数据结构·c++·算法