题目:Binary Tree Inorder Traversal

二叉树的中序遍历,和前序、中序一样的处理方式,代码见下:

 struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x): val(x), left(NULL),right(NULL) {}
}; vector<int> preorderTraversal(TreeNode *root) //非递归的中序遍历(用栈实现)
{
if (NULL == root) {
return vector<int>();
} stack<TreeNode *> tree_stack;
vector<int> tree_vector; TreeNode *pTemp = root;
while (pTemp || !tree_stack.empty()) {
while (pTemp) {
tree_stack.push(pTemp);
pTemp = pTemp->left;
}
if (!tree_stack.empty()) {
pTemp = tree_stack.top();
tree_vector.push_back(pTemp->val);
tree_stack.pop();
pTemp = pTemp->right;
}
}
return tree_vector;
}

LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium的更多相关文章

  1. 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)

    题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...

  2. [Leetcode] Binary tree inorder traversal二叉树中序遍历

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

  3. [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

  4. LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)

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

  5. [Leetcode] Binary tree postorder traversal二叉树后序遍历

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

  6. 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)

    题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...

  7. LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium

    题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...

  8. [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历

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

  9. LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard

    题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...

随机推荐

  1. Eclipse中创建一个新的SpringBoot项目

    在Eclipse中创建一个新的spring Boot项目: 1. 首先在Eclipse中安装STS插件:在Eclipse主窗口中点击 Help -> Eclipse Marketplace... ...

  2. VMware虚拟机配置嵌套虚拟化

    VMware虚拟机下创建kvm-sever,server下继续创建kvm虚拟机(嵌套虚拟化),返回libvirt错误解决办法:SSH连接VMwarevi /etc/vmware/config增加一行设 ...

  3. python 网络编程 缓冲和粘包

    tcp:属于长连接,与一个客户端进行连接了以后,其他的客户端要等待,要连接另外一个,必须优雅的断开前面这个客户端的连接. 允许地址重用:在bind IP地址和端口之前加上,# server.setso ...

  4. 好看的alert弹出框sweetalert

    转载:https://www.cnblogs.com/lamp01/p/7215408.html

  5. socketserver 模块并发

    socketserver是将socket封装的类. 实例: 服务端: import socketserver class Myserver(socketserver.BaseRequestHandle ...

  6. zabbix钉钉报警

    我们在钉钉上建立群聊,然后在群聊上添加钉钉机器人: 编写,脚本需要放在zabbix 的alertscripts目录下(如果不知道该目录的位置,可以使用find命令查找) find / -iname a ...

  7. c#: 简单的日志管理类(TextWriterTraceListener)

    以c#实现轻量级的日志管理,着实简单,置一静态类记之: /// <summary> /// 日志管理 /// </summary> public static class Lo ...

  8. Head First Servlets & JSP 学习笔记 第十一章 —— Web应用部署

    jar:java archive(java归档) war:web archive(web归档) war文件只是Web应用结构的一个快照,采用了一种更可移植的压缩形式(它实际上就是一个jar文件).建立 ...

  9. django 在centos 7 下 指定ip地址和端口 报错问题

    windows environment: python manage.py runserver host:port centos environment: python manage.py runse ...

  10. linux就该这么学,第五课,

    今天讲的比较难理解,要重预习和复习 今天讲了2个多小进,主要讲了SHELL,shell的组成:第一行为脚本声明 #!/bin/bash   ,第二行为脚本的注释信息,第三为 脚本的执行语句 接收用户参 ...