Total Accepted: 98729 Total Submissions: 261539 Difficulty: Medium

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?

/**
* 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> res;
stack<TreeNode*> stk;
while(root || !stk.empty()){
while(root){
stk.push(root);
root=root->left;
}
if(!stk.empty()){
root = stk.top();
stk.pop();
res.push_back(root->val);
root = root->right;
}
}
return res;
}
};

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

  1. leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree

    leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...

  2. LeetCode & tree & binary tree

    LeetCode & tree & binary tree 树 & 二叉树 refs https://leetcode.com/problemset/all/?topicSlu ...

  3. [Tree]Binary Tree Preorder Traversal

    Total Accepted: 97599 Total Submissions: 257736 Difficulty: Medium Given a binary tree, return the p ...

  4. CCI4.4/LintCode Balanced Binary Tree, Binary Tree, Binary Search Tree

    Binary Tree: 0到2个子节点; Binary Search Tree: 所有左边的子节点 < node自身 < 所有右边的子节点: 1. Full类型: 除最下面一层外, 每一 ...

  5. LeetCode_Lowest Common Ancestor of a Binary Search Tree (Binary Tree)

    Lowest Common Ancestor of a Binary Search Tree 一.题目描写叙述 二.思路及代码 二叉搜索树有个性质:左子树的值都比根节点小,右子树的值比根节点大.那么我 ...

  6. [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历

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

  7. Leetcode 94. Binary Tree Inorder Traversal

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

  8. Binary Tree Inorder Traversal

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

  9. Leetcode Binary Tree Inorder Traversal

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

随机推荐

  1. HTML5新增的主体元素和新增的非主体结构元素

    HTML5新增的主体元素 article元素 article元素表示文档.页面或应用程序中独立的.完整的.可以独自被外部引用的内容.它可以是一篇博客或者报刊中的文章,一篇论坛帖子.一段用户评论或独立的 ...

  2. 在IIS上Office Word下载失败,检索 COM 类工厂中 CLSID 为000209FF的组件失败,80070005 拒绝访问。

    最近在做一个网站时,有一个下载word文档功能,在本地直接调试是可以下载的,但部署到IIS上就出现问题了. 出现问题如下:Error:下载简历方法出错:检索 COM 类工厂中 CLSID 为 {000 ...

  3. 深入char、varchar、text和nchar、nvarchar、ntext的区别详解

    很多开发者进行数据库设计的时候往往并没有太多的考虑char, varchar类型,有的是根本就没注意,因为存储价格变得越来越便宜了,忘记了最开始的一些基本设计理论和原则,这点让我想到了现在的年轻人,大 ...

  4. relativelayout常用属性

    ===================================================================================== 整理于http://naot ...

  5. 测试工具:insure++

    CSDN资源:http://www.csdn.net/tag/insure%252B%252B 安装. 1,简介:http://baike.baidu.com/link?url=bCcoWd3xi07 ...

  6. const的一些总结

    const的一些总结 采用const符号常量写出来的代码更容易维护,有些函数只读不写: 1 常变量: const 类型说明符 变量名 2 常引用: const 类型说明符 &变量名 3 常成员 ...

  7. AdapterView及其子类之四:基于ListView及SimpleAdapter实现列表

    代码请见SimpleAdapterDemo.zip. 步骤如下: 1.创建主布局文件 <RelativeLayout xmlns:android="http://schemas.and ...

  8. WebService使用的一些总结

    什么是WebService: 这个不用我在这里废话,网上的资料一搜一大把,如果你没有接触过这方面的知识,你可以先去网上查一下.这里我只想说一下我印象比较深刻的几点: WebService是基于soap ...

  9. python图片小爬虫

    import re import urllib import os def rename(name): name = name + '.jpg' return name def getHtml(url ...

  10. Ubuntu 查找命令

    Ubuntu 查找文件夹 使用find命令查找find / -name 文件夹名称 -type d找到结果中含有路径 查找命令 从根目录开始查找所有扩展名为.log的文本文件,并找出包含”ERROR” ...