# 94. Binary Tree Inorder Traversal

114人阅读 评论(0)

Given a binary tree, return the inorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
\
2
/
3


return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

   1
/ \
2   3
/
4
\
5

The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {

List<Integer> list = new ArrayList<Integer>();

Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode cur = root;

while(cur!=null || !stack.empty()){
while(cur!=null){
cur = cur.left;
}
cur = stack.pop();
cur = cur.right;
}
return list;
}
}

C++版：0ms

/**
* 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> result;
stack<TreeNode*> inStack;
TreeNode* curNode = root;

while(curNode || !inStack.empty()){
if(curNode){
inStack.push(curNode);
curNode = curNode->left;
}else{
curNode = inStack.top();
inStack.pop();
result.push_back(curNode->val);
curNode = curNode->right;
}
}

return result;
}
};


/**
* 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> result;
inorderTraversal(root, result);

return result;
}

void inorderTraversal(TreeNode* root, vector<int>& result){
if(!root){
return;
}
inorderTraversal(root->left, result);
result.push_back(root->val);
inorderTraversal(root->right, result);
}
};


1. O(1)空间复杂度，即只能使用常数空间；

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> result;
TreeNode* curNode = root;

while(curNode){
if(curNode->left){
TreeNode* prev = curNode->left;
while(prev->right && prev->right!=curNode){
prev = prev->right;
}
if(!(prev->right)){
prev->right = curNode;
curNode = curNode->left;
}else{
prev->right = NULL;
result.push_back(curNode->val);
curNode = curNode->right;
}
}else{
result.push_back(curNode->val);
curNode = curNode->right;
}
}

return result;
}
};


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：49484次
• 积分：917
• 等级：
• 排名：千里之外
• 原创：8篇
• 转载：242篇
• 译文：0篇
• 评论：5条
文章分类
阅读排行