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. 【贪心】10.24assassin

    题目分析 没有题目分析…… 寄存一下神奇反悔贪心 #include<bits/stdc++.h> ; struct node { int a,b; node(, ):a(x),b(y) { ...

  2. pandas处理大文本数据

    当数据文件是百万级数据时,设置chunksize来分批次处理数据 案例:美国总统竞选时的数据分析 读取数据 import numpy as np import pandas as pdfrom pan ...

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

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

  4. Ubuntu 18.04 下用命令行安装Sublime

    介绍: 添加来源: $ wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add - $ sud ...

  5. 拖拽图片到另一个div里

    HTML代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  6. Python使用gevent实现协程

    Python中多任务的实现可以使用进程和线程,也可以使用协程. 一.协程介绍 协程,又称微线程.英文名Coroutine.协程是Python语言中所特有的,在其他语言中没有. 协程是python中另外 ...

  7. Hibernate知识梳理

    一.SessionFactory接口 是单个数据库映射关系(ORM)经过编译后的内存镜像.SessionFactory(的实例)作为应用中的一个全局对象(工厂),可以随处打开/创建一个session, ...

  8. dict 方法总结整理

    #!/usr/bin/env python __author__ = "lrtao2010" #Python 3.7.0 字典常用方法 #字典的key是唯一的,且不能被修改,val ...

  9. (转)可简化iOS 应用程序开发的6个Xcode小技巧

    Xcode是iPhone和iPad开发者用来编码或者开发iOS app的IDE.Xcode有很多小巧但很有用的功能,很多时候我们可能没有注意到它们,也或者我们没有在合适的水平使用这些功能简化我们的iO ...

  10. UVA - 1220 Party at Hali-Bula (树形DP)

    有 n 个员工,n-1个从属关系. 不能同时选择某个员工和他的直接上司,问最多可以选多少人,以及选法是否唯一. 树上的最大独立集问题.只不过多了一个判断唯一性. dp[u][0]表示不选这个点的状态, ...