ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

LeetCode HOT100 - 二叉树的中序遍历

LeetCode HOT100 - 二叉树的中序遍历

简单搜索

左中右

/*** 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:vector<int> inorderTraversal(TreeNode* root) {vector<int> ans;auto dfs = [&](this auto&& self, TreeNode* x) -> void {if (!x) {return;}self(x->left);ans.emplace_back(x->val);self(x->right);};dfs(root);return ans;}
};
返回列表