Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
58 lines (56 sloc) 1.19 KB
/*
* @lc app=leetcode.cn id=94 lang=cpp
*
* [94] 二叉树的中序遍历
*
* https://leetcode-cn.com/problems/binary-tree-inorder-traversal/description/
*
* algorithms
* Medium (63.54%)
* Total Accepted: 19K
* Total Submissions: 29.9K
* Testcase Example: '[1,null,2,3]'
*
* 给定一个二叉树,返回它的中序 遍历。
*
* 示例:
*
* 输入: [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> ret;
order(root,ret);
return ret;
}
void order(TreeNode* root,vector<int>& ret)
{
if(!root) return;
order(root->left,ret);
ret.push_back(root->val);
order(root->right,ret);
}
};
You can’t perform that action at this time.