标签:col return tree 二叉树 void style pre roo ret
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3] 1 2 / 3 输出: [1,3,2]
递归:
/** * 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> vec; LDR(root,vec); return vec; } void LDR(TreeNode* root, vector<int> &vec){ if(root == NULL){ return; } LDR(root->left, vec); vec.emplace_back(root->val); LDR(root->right, vec); } };
标签:col return tree 二叉树 void style pre roo ret
原文地址:https://www.cnblogs.com/olajennings/p/12487957.html