http://oj.leetcode.com/problems/binary-tree-inorder-traversal/

树的中序遍历,递归方法,和非递归方法。

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
void subfunction(TreeNode *root,vector<int> &result)
{
if(root == NULL)
return;
subfunction(root->left,result);
result.push_back(root->val);
subfunction(root->right,result);
}
public:
vector<int> inorderTraversal(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
vector<int> result;
result.clear();
subfunction(root,result);
return result; }
};
 #include <iostream>;
#include<stack>
#include<vector>
using namespace std; //Definition for binary tree
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution { private:
void instack(TreeNode * root,stack< TreeNode*> &datastack )
{
if(root ==NULL)
return;
while(root ->left!=NULL)
{
datastack.push(root ->left);
root = root ->left;
}
}
public:
vector< int> inorderTraversal(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
stack<TreeNode *> datastack; vector<int> result;
result.clear();
if(root==NULL)
return result;
datastack.push(root);
instack( root,datastack); TreeNode* tempNode = (TreeNode*)malloc(sizeof(TreeNode));
while(datastack.empty()==false)
{
tempNode = datastack.top();
result.push_back(tempNode->val);
datastack.pop();
if(tempNode->right==NULL )
continue;
else
{
datastack.push(tempNode->right);
instack(tempNode->right,datastack);
}
}
return result;
}
};
#include<iostream>
#include"cla.h"
using namespace std; int main()
{
TreeNode *myroot = (TreeNode*)malloc(sizeof(TreeNode));
myroot->val = ; TreeNode *myroot2 = (TreeNode*)malloc(sizeof(TreeNode));
myroot2->val = -;
myroot2->left = NULL;
myroot2->right = NULL; TreeNode *myroot3 = (TreeNode*)malloc(sizeof(TreeNode));
myroot3->val = ;
myroot3->left = NULL;
myroot3->right = NULL; myroot->left = myroot2;
myroot->right = myroot3; TreeNode *myroot4 = (TreeNode*)malloc(sizeof(TreeNode));
/*myroot4->val = 9;*/
/*myroot3->right =myroot4;*/
myroot4->left = NULL;
myroot4->right = NULL; Solution *pSolution = new Solution; vector<int> result;
result = pSolution->inorderTraversal(NULL);
for(int i = ;i<result.size();i++)
cout<<result[i]<<" "; if(pSolution!=NULL)
delete pSolution;
pSolution = NULL; return ;
}

LeetCode OJ——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 tr ...

  6. Leetcode 94. 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 ----- java

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

  8. Java [Leetcode 94]Binary Tree Inorder Traversal

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

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

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

随机推荐

  1. php进行文件的强制下载

    浏览器下载文件,例如在浏览器中可以直接打开的文件(.gif /.txt等).在进行文件下载操作时,默认是通过浏览器直接打开,而不是下载保存文件.并且通过这种方法下载文件可以不暴漏下载文件所在的路径,可 ...

  2. 03IO端口寻址和访问控制方式

    1. I/O端口和寻址 CPU 为了访问 I/O 接口控制器或者控制卡上的数据和状态信息,需要首先指定他们的地址.这种地址就称为I/O端口地址或简称端口.通常一个 I/O 控制器包含访问数据的数据端口 ...

  3. 13Shell脚本—编写简单脚本

    1. 概述 Shell脚本命令的工作方式有两种:交互式和批处理. 交互式(Interrctive): 用户每输入一条命令就立即执行. 批处理(Batch): 由用户事先编写好一个完整的 Shell 脚 ...

  4. overtrue/wechat 包 由 sys_get_temp_dir 引发的 the directory "c:\Windows" is not writable

    vendor\overtrue\wechat\src\Foundation\Application.php registerBase 方法 在初始化属性时 $this['cache'] = funct ...

  5. mysql的字符串连接符

    以前用SQL Server 连接字符串是用“+”,现在数据库用mysql,写个累加两个字段值SQL语句居然不支持"+",郁闷了半天在网上查下,才知道mysql里的+是数字相加的操作 ...

  6. 如何使用 HTML5 的picture元素处理响应式图片

    来自: http://www.w3cplus.com/html5/quick-tip-how-to-use-html5-picture-for-responsive-images.html 图片在响应 ...

  7. nrf51822微信开发2:[转]airkiss/airsync介绍

    "微信蓝牙"专题共分为8部分 1.airkiss/airsync介绍 2.eclipes的j2ee软件使用教程 3.微信公众号使用Dome(airkiss/airsync) 4.新 ...

  8. hdu4864不是一般的贪心

    题目表达的非常清楚,也不绕弯刚开始以为最大权匹配,仔细一想不对,这题的数据双循环建图都会爆,只能先贪心试一下,但一想贪心也要双循环啊,怎么搞? 想了好久没头绪,后来经学长提醒,可以把没用到的先记录下来 ...

  9. CSS(非布局样式)

    CSS(非布局样式) 问题1.CSS样式(选择器)的优先级 1.计算权重 2.!important 3.内联样式比外嵌样式高 4.后写的优先级高 问题2.雪碧图的作用 1.减少 HTTP 请求数,提高 ...

  10. selenium - js日历控件处理

    # 13. js处理日历控件 ''' 在web自动化的工程中,日历控制大约分为两种: 1. 可以直接输入日期 2. 通过日历控件选择日期 基本思路: 利用js去掉readonly属性,然后直接输入时间 ...