LeetCode OJ——Binary Tree Inorder Traversal
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的更多相关文章
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- 【LeetCode】Binary Tree Inorder Traversal
Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...
- leetcode -day29 Binary Tree Inorder Traversal & Restore IP Addresses
1. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...
- leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- Leetcode 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- leetcode 94 Binary Tree Inorder Traversal ----- java
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Java [Leetcode 94]Binary Tree Inorder Traversal
题目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given bina ...
- Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
随机推荐
- 《Spring源码深度解析》第三章 默认标签的解析
上一章提到了,默认标签和自定义标签要分开解析.本章重点介绍默认标签的解析.在 DefaultBeanDefinitionDocumentReader 中: private void parseDefa ...
- Python 简单购物程序
# Author:Eric Zhao# -*- coding:utf-8 -*-'''需求:启动程序后,让用户输入工资,然后打印商品列表允许用户根据商品编号购买商品用户选择商品后,检测余额是否够,够就 ...
- Java技术——Java反射机制分析
)生成动态代理. 2. Java反射API 反射API用来生成在当前Java虚拟机中的类.接口或者对象的信息. Class类:反射的核心类,可以获取类的属性,方法等内容信息. Field类:Java. ...
- HDU 5473 There was a kingdom 凸包 DP
题意: 给出平面上n个点的坐标,选k个点,使得这k个点围起来的面积最大. 分析: 参考了 叉姐的分析 和 不慌不忙菊苣的代码 思路我都懂,但是DP的部分还是不太会写. 我体会了一下其中含义,也许这样可 ...
- HDU 5378 树上的概率DP Leader in Tree Land
官方题解: 可以用求概率的思想来解决这个问题.令以i号节点为根的子树为第i棵子树,设这颗子树恰好有sz[i]个点.那么第i个点是第i棵子树最大值的概率为1/sz[i],不是最大值的概率为(sz[i]- ...
- git 强制回到以前版本
git reset dfd3e36a641340a0b86f811df869c4375fabeff2 --hard
- 触屏版轻量级分页插件jqPagination分享
说到HTML5和jquery上的分页问题,优秀的分页插件网上一抓一大把,然而同时适合兼容在Ipad和手机端的网站分页却不是特别多. 或许有人会说,触屏现在流行下拉底部后加载下一页内容,类似微博和QQ空 ...
- TensorFlow学习笔记(6):TensorBoard之Embeddings
本文基于TensorFlow官网的How-Tos写成. TensorBoard是TensorFlow自带的一个可视化工具,Embeddings是其中的一个功能,用于在二维或三维空间对高维数据进行探索. ...
- tensorflow——MNIST机器学习入门
将这里的代码在项目中执行下载并安装数据集. 执行下面代码,训练.并评估模型: # _*_coding:utf-8_*_ import inputdata mnist = inputdata.read_ ...
- PHP魔术函数、魔术常量、预定义常量
一.魔术函数(13个) 1.__construct() 实例化对象时被调用, 当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用. 2.__des ...