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. 【Mysql】Mysql主从库搭建过程(爬完坑后整理所得)

    Mysql主从数据库搭建流程 新手开始学习mysql主从库,遇到一些问题,总结后写出以下流程 下面以5.7.23版本为例介绍 第一步:去官网下载5.7.23版本的免安装压缩包形式的mysql文件,贴上 ...

  2. (66)zabbix导入/导出配置文件

    通过导入/导出zabbix配置文件,我们可以将自己写好的模板等配置在网络上分享,我们也可以导入网络上分享的配置文件 配置文件有两种格式,分为为xml与json,通过zabbix管理界面可以导出xml, ...

  3. k8s Pod的自动水平伸缩(HPA)

    我们知道,当访问量或资源需求过高时,使用:kubectl scale命令可以实现对pod的快速伸缩功能 但是我们平时工作中我们并不能提前预知访问量有多少,资源需求多少. 这就很麻烦了,总不能为了需求总 ...

  4. destoon去除编辑器替换图片删除原图功能,删除信息删除相关图片功能

    去除这些功能会造成大量垃圾图片,但是客户存在大量复制内容,其中图片一样,为了防止客户替换其中一个图片或者删除信息 造成其他复制信息图片丢失 去除文章模型级联图片功能. 对应模块class.php se ...

  5. REST Framework 处理一个超链接序列化问题

    问题简述 翻译: 不正确的配置 无法使用视图名称“snippet-detail”解析超链接关系的URL.您可能没有在API中包含相关的模型,或者在该字段上错误地配置了' lookup field '属 ...

  6. LeetCode(274)H-Index

    题目 Given an array of citations (each citation is a non-negative integer) of a researcher, write a fu ...

  7. 笔记-python-urllib

    笔记-python-urllib 1.      简介 PYTHON3中将urllib,urllib2整合到URLLIB中 包括以下模块 urllib.request 请求模块(核心) urllib. ...

  8. luogu3383 【模板】线性筛素数

    如果prime[i]是k的因子,那么[k * (在prime[i]以后的质数)]等于[prime[i]*(k/prime[i])*(这个质数)],一定被筛过了,所以这里可以break. #includ ...

  9. 常见的Linux目录及其含义

    /                                   系统根目录,通常不会在这里存放文件 . /bin                              二进制目录,存放许多 ...

  10. python--命名规范及常见的数据类型

    1.python的命名规范 (1)不能以数字开头,不能出现中文. (2)命名以字母开头,包含数字,字母(区分大小写),下划线. (3)不能包含关键字,见名知意. 2.python常见的数据类型 (1) ...