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 curl使用例子

    PHP支持的由Daniel Stenberg创建的libcurl库允许你与各种的服务器使用各种类型的协议进行连接和通讯.libcurl目前支持http.https.ftp.gopher.telnet. ...

  2. Linux-Mysql8.0

    Mysql8.0.12 基本操作 解释 命令 安装服务端 yum install mysql-community-server 启动 service mysqld start/restart 停止 s ...

  3. Unity基础-图形渲染

    图形渲染-Camera Camera下的Clear Flags:Skybox,Don't Clear,Depth only(深度),Solid Color(固定颜色) Culling Mask:渲染层 ...

  4. Linux 文本编辑常用快捷键

    一.编辑模式 vim有三种编辑模式 1. i 进入文本编辑模式 2. esc 进入命令编辑模式 命令编辑状态下 dd删除整行 3. :进入底行模式 底行模式状态  输入q 退出 w保存  wq 保存并 ...

  5. HDU:2586-How far away

    How far away Time limit1000 ms Memory limit32768 kB Problem Description There are n houses in the vi ...

  6. 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)

    A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...

  7. Root CA certificate:ApacheJMeterTemporaryRootCA.crt created in JMeter bin directory

    今天学习jmeter录制,在点击start之后弹出: 且在jmeter安装目录里确实生成了ApacheJMeterTemporaryRootCA.crt文件 上网查询官方文档http://120.52 ...

  8. Jenkins自动化搭建测试环境(一)

    Jenkins基础 首先上官网jenkins.io上下载最新的Jenkins war包 将下载完成的war包解压 java -jar jenkins.war 接下来使用浏览器访问localhost:8 ...

  9. LoadRunner 手动关联

    步骤: 1.同样的流程录制两遍,保存在不同的项目中, 2.点击Tools >> Compare with Script 3.选择要比对的脚本 3.找辨识码 4.打开辨识码所在网页,查看源代 ...

  10. 【JavaScript】关于 setInterval() 调用函数方法的一次实验

    实验主题: setInterval() 方法是 JS 中比较常用的一个方法.setInterval() 方法可以按照指定的周期 ( 毫秒 ) 来调用函数方法或计算表达式. setInterval() ...