给定树根root。实现中序遍历,也就是左根右。

用递归的话,很简单,左边的返回值加上root的再加上右边的就行。

我自己写的有点挫:

/**
* Definition for binary tree
* 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> lf, ri, ans;
if (root == NULL) return ans;
lf = inorderTraversal(root -> left);
ri = inorderTraversal(root -> right);
lf.push_back(root -> val);
for (int i = ; i < ri.size(); ++i)
{
lf.push_back(ri[i]);
}
return lf;
}
};

其实可以写简单一些

class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> vi;
inHelper(root, vi);
return vi;
} void inHelper(TreeNode *node, vector<int>& vi)
{
if(node == nullptr) return;
inHelper(node->left, vi);
vi.push_back(node->val);
inHelper(node->right, vi);
}
};

题目要求如果不用递归的话,用如下leetcode上的,利用栈,很妙。

vector<int> inorderTraversal(TreeNode *root)
{
vector<int> rs;
if (!root) return rs;
stack<TreeNode *> stk;
TreeNode *p = root;
while (!stk.empty() || p)
{
if (p)
{
stk.push(p);
p = p->left;
}
else
{
p = stk.top();
stk.pop();
rs.push_back(p->val);
p = p->right;
}
}
return rs;
}

2014-12-13

/**
* Definition for binary tree
* 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> perm;
if (root == NULL) return perm;
stack<TreeNode *> sta; TreeNode *p = root; while(!sta.empty() || p)
{
while (p)
{
sta.push(p);
p = p -> left;
}
if (!sta.empty())
{
p = sta.top();
sta.pop();
perm.push_back(p -> val);
p = p -> right;
}
}
return perm;
}
};

leetcode[96] Binary Tree Inorder Traversal的更多相关文章

  1. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

  2. 49. leetcode 94. Binary Tree Inorder Traversal

    94. Binary Tree Inorder Traversal    二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack

  3. 【LeetCode】Binary Tree Inorder Traversal

    Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...

  4. leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

    1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...

  5. Leetcode 94. Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  6. leetcode 94 Binary Tree Inorder Traversal ----- java

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  7. Java [Leetcode 94]Binary Tree Inorder Traversal

    题目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given bina ...

  8. Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

  9. LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...

随机推荐

  1. Linux 常用命令汇总

    1.shutdown -s 时间 如果是想马上关机就直接输入0或者now: 2.init 0 这个是运行级别关机: 3.halt 这个命令不是很好用: 4.power off 这个命令也是很好用的. ...

  2. VS路宏 vc++于OutDir、ProjectDir、SolutionDir不同的路径

    说明 $(RemoteMachine) 设置为"调试"属性页上"远程计算机"属性的值.有关很多其它信息,请參见更改用于 C/C++ 调试配置的项目设置. $(R ...

  3. remine chart2安装

    http://blog.csdn.net/kufeiyun/article/details/9213911

  4. OUC_OptKernel_oshixiaoxiliu_好题推荐

    poj1112 Team Them Up! 补图二分图+dp记录路径codeforces 256A Almost Arithmetical Progression dp或暴力 dp[i][j] = d ...

  5. poj 2804 字典 (特里 要么 快排+二分法)

    2804:词典 总时间限制:  3000ms  内存限制:  65536kB 描写叙述 你旅游到了一个国外的城市.那里的人们说的外国语言你不能理解.只是幸运的是,你有一本词典能够帮助你. 输入 首先输 ...

  6. mongodb迁移

    A机器上有mongodb服务,A机器要废,于是迁至B. 简单起见,依旧是在A上ps auxwww|grep mongo找到正在执行的进程: /home/admin/mongodb/mongodb-li ...

  7. C#如何设置session过期时间

    1.操作系统  步骤:开始——〉管理工具——〉Internet信息服务(IIS)管理器——〉网站——〉默认网站——〉  右键“属性”——〉主目录——〉配置——〉选项——〉启用会话状态——〉会话超时(在 ...

  8. 学习javascript 的一点感想

    原文:学习javascript 的一点感想 //动态性是指,在一个Javascript对象中,要为一个属性赋值,我们不必事先创建一个字段,只需要在使用的时候做赋值操作即可,如下例:var obj=ne ...

  9. 随手记UIKit Dynamics

    以今年的优势WWDC品行,我记得一些明年的元素.一些博客上找到了新的功能没有被记录.认为iOS 8全力以赴.iOS 7该属性不随手记录为时已晚 :) 参考WWDC 2013的Session Video ...

  10. python_基础学习_03_正则替换文本(re.sub)

    python的正则表达式模块是re,替换相关的方法是sub. 例如我们要做如下的替换将所有的 替换为空格,可以通过下面代码实现: import re input = 'hello world' #第一 ...