Leetcode Binary Tree Inorder Traversal
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?
递归解决方案
class Solution {
public:
    vector<int> res;
    void inorder(TreeNode* root){
        if(root == NULL) return;
        inorder(root->left);
        res.push_back(root->val);
        inorder(root->right);
    }
    vector<int> inorderTraversal(TreeNode *root) {
        inorder(root);
        return res;
    }
};
递归中序遍历
非递归解决方案
class Solution {
public:
    vector<int> inorderTraversal(TreeNode *root) {
        vector<int> res;
        if(root == NULL) return res;
        stack<TreeNode *> nodeStack;
        TreeNode *current = root;
        while(!nodeStack.empty() || current ){
            if(current != NULL){
                nodeStack.push(current);
                current = current->left;
            }else{
                current = nodeStack.top();nodeStack.pop();
                res.push_back(current->val);
                current = current->right;
            }
        }
        return res;
    }
};
非递归中序遍历
Leetcode Binary Tree Inorder Traversal的更多相关文章
- LeetCode: Binary Tree Inorder Traversal 解题报告
		
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
 - [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
		
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
 - [Leetcode] Binary tree inorder traversal二叉树中序遍历
		
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
 - [LeetCode] Binary Tree Inorder Traversal 中序排序
		
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
 - leetcode Binary Tree Inorder Traversal python
		
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
 - [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
		
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
 - [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
		
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
 - LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
		
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
 - 49. leetcode 94. Binary Tree Inorder Traversal
		
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
 
随机推荐
- 索引的重载 str["name"]  str[i]
			
class Program { static void Main(string[] args) { IndexClass names = new IndexClass(); names[] = &qu ...
 - Qt Designer怎样加入资源文件
			
Qt Designer中如果在设计UI界面的时候要加入一些图素,图标等资源的时候是不能直接添加进去的,需要在Qt开发目录下编写QRC文件 qrc文件格式如下: <RCC> <qres ...
 - mysql5.7.11安装配置
			
1.下载安装包. mysql-5.7.11版本: http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.11-winx64.zip 2.拷贝到任意盘: ...
 - Activity有四种加载模式(转)
			
Activity有四种加载模式: standard singleTop singleTask singleInstance 在多Activity开发中,有可能是自己应用之间的Activity跳转,或者 ...
 - Oracle中“行转列”的实现方式
			
在报表的开发当中,难免会遇到行转列的问题. 以Oracle中scott的emp为例,统计各职位的人员在各部门的人数分布情况,就可以用“行转列”: scott的emp的原始数据为: EMPNO ENAM ...
 - 【工具】Git
			
1.安装好Git以后,在开始菜单里找到Git->Git Bash,弹出一个命令窗口 2.设置邮箱 . 3.创建文件夹 4.创建版本库 5.将文件添加到缓存区中去 6.提交文件 7.检查是否还有文 ...
 - poj 2063 Investmen  完全背包
			
这个题的想法不难,两个点: 1 是完全背包 2 是考虑/1000,降低复杂度 但是提交的时候反复的wa,最后找问题原来是dp开小了,可是dp本来开1005,后来开到100030过了.哎,如果没有时 ...
 - jekyll bootstrap
			
你还在纠结使用那个博客系统吗?或者为没有自己的服务器和专属域名而感到无奈?也许jekyll bootstrap是你的最终解决方案,使用它,你就可以像写代码一样写博客.本文将为你详细介绍ubuntu下的 ...
 - hdu1710 二叉树的遍历
			
Problem Description 已知前序和中序 求后序 Input The input contains several test cases. The first line of each ...
 - 彻底搞懂Html5本地存储技术(一)
			
一.H5之前客户端本地存储的实现 1. cookies cookies的应用比较广泛,但有以下几个问题: (1)每次http请求头上会带着,浪费资源 (2)每个域名客户端只能存储4K大小 (3)会造成 ...