Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
76 lines (75 sloc) 1.66 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) {}
* };
*/
//Morris Traversal
//O1,非递归+不用栈,Threaded binary tree
// Non-recursion and no stack
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root)
{
vector<int> res;
if (!root) return res;
TreeNode *cur, *pre;
cur = root;
while (cur)
{
if (!cur->left)
{
res.push_back(cur->val);
cur = cur->right;
}
else {
pre = cur->left;
while (pre->right && pre->right != cur) pre = pre->right;
if (!pre->right)
{
pre->right = cur;
cur = cur->left;
}
else
{
pre->right = NULL;
res.push_back(cur->val);
cur = cur->right;
}
}
}
return res;
}
};
You can’t perform that action at this time.