码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 94 Binary Tree Inorder Traversal

时间:2015-06-17 13:18:18      阅读:107      评论:0      收藏: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?

注意 In-order 是中根遍历

1. 如果用递归的就 左根右

 1 class Solution {
 2 public:
 3     vector<int> inorderTraversal(TreeNode* root) {
 4         vector<int> v;
 5         if (root == NULL)
 6             return v;
 7         if (root->left == NULL && root ->right == NULL)
 8         {
 9             v.push_back(root->val);
10             return v; 
11         }
12         vector<int> left = inorderTraversal(root->left);
13         vector<int> right = inorderTraversal(root->right);
14 
15         v.insert(v.end(), left.begin(), left.end());
16         v.push_back(root->val);
17         v.insert(v.end(), right.begin(), right.end());
18 
19         return v; 
20     }
21 };

 

2. 非递归算法: 

a 先把左子树全部压进去,然后开始弹,访问它,再把它的右子树压进去,其实开始弹出来的时候,那个点即可以看作孩子,也可以看做根.所以是 左-》根》右

 1 class Solution {
 2 public:
 3     vector<int> inorderTraversal(TreeNode* root) {
 4         vector<int> v;
 5         if (root == NULL)
 6             return v; 
 7         TreeNode * p = root; 
 8         stack<TreeNode*> s; 
 9         while(p)
10         {
11             while(p) //把左边的树一直压进去
12             {
13                 s.push(p);
14                 p = p->left; 
15             }
16             while (!s.empty()) // 弹出一个点,看有没有右孩子,有的话,拿出来作为根来遍历
17             {
18                 TreeNode * cur = s.top();
19                 s.pop();
20                 v.push_back(cur->val);
21                 if(cur->right)
22                 {
23                     p = cur->right;
24                     break;
25                 }
26             }
27         }
28         return v; 
29     }
30 };

 

b  用一个位来标记,根有没有被遍历过。 如果是0 就没有遍历过,把他的右孩子 还有 左孩子 放进去。 如果是1 就遍历过,然后直接输出 处理。

这个方法比较简单,也比较快,推荐记忆。

 1 class Solution {
 2 public:
 3     vector<int> inorderTraversal(TreeNode* root) {
 4         std::vector<int> v;
 5         if (root == NULL)
 6             return v; 
 7         if (root ->left == NULL && root -> right == NULL ) {
 8             v.push_back(root->val);
 9             return v; 
10         } 
11         stack <pair<TreeNode*, int> > s;
12         s.push(make_pair(root,0));
13         while(!s.empty())
14         {
15             TreeNode * p = s.top().first;
16             int index = s.top().second; 
17             s.pop();
18             if (!index)
19             {
20                 if(p->right)
21                     s.push(make_pair(p->right,0));
22                 s.push(make_pair(p,1));
23                 if(p->left)
24                     s.push(make_pair(p->left,0));
25             } else {
26                 v.push_back(p->val);
27             }
28         } 
29         return v; 
30     }
31 };

 

Leetcode 94 Binary Tree Inorder Traversal

标签:

原文地址:http://www.cnblogs.com/zhuguanyu33/p/4582921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!