LC 144. / 94. / 145. Binary Tree Preorder/ Inorder/ PostOrder Traversal
题目描述
前序排列 :根-左-右
中序排列: 左-根-右
后序排列:左-右-根
参考答案
// PreOrder
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
vector<int> res; public:
vector<int> preorderTraversal(TreeNode* root) {
foo(root);
return res;
}
void foo(TreeNode* root){
if(root == NULL)
return; res.push_back(root->val);
foo(root->left);
foo(root->right);
}
}; // InOrder
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
if(root == NULL){
return vector<int>();
}
vector<int> result; // 创建结果
stack<TreeNode *> s;// 创建堆栈
TreeNode *p = root; // 临时让p为root
// 寻找p最左边的node
while(p!=NULL){
s.push(p); // 从root开始,将左边的node推入stack
p = p->left;// 更新p为左node
}
// s 为全是左节点的stack
// 对 s进行循环操作
while(!s.empty()){
// 将最最左边的,推入stack
p = s.top();
result.push_back(p->val);
s.pop(); // 自己消失了
// 然后观察这个的右边node
if(p->right != NULL){
p = p->right;
while(p!=NULL){ //观察node的左边
s.push(p);
p = p->left;
}
}
}
return result;
}
}; // PostOrder class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
if(root == NULL){
return vector<int>();
}
vector<int> result;
stack<TreeNode *> s; s.push(root);
while(!s.empty()){
TreeNode *temp = s.top();
result.push_back(temp->val);
s.pop();
if(temp->left!=NULL){
s.push(temp->left);
}
if(temp->right!= NULL){
s.push(temp->right);
}
}
reverse(result.begin(), result.end());
return result; }
};
LC 144. / 94. / 145. Binary Tree Preorder/ Inorder/ PostOrder Traversal的更多相关文章
- LeetCode之“树”:Binary Tree Preorder && Inorder && Postorder Traversal
Binary Tree Preorder Traversal 题目链接 题目要求: Given a binary tree, return the preorder traversal of its ...
- [LeetCode] Binary Tree Preorder/Inorder/Postorder Traversal
前中后遍历 递归版 /* Recursive solution */ class Solution { public: vector<int> preorderTraversal(Tree ...
- 【题解】【BT】【Leetcode】Binary Tree Preorder/Inorder/Postorder (Iterative Solution)
[Inorder Traversal] Given a binary tree, return the inorder traversal of its nodes' values. For exam ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- 145. Binary Tree Postorder Traversal
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- Binary Tree Preorder Traversal and Binary Tree Postorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
随机推荐
- 超轻量级虚拟终端sakura和tilda
一.安装: manjaro:pacman -S sakura ubunt:sudo apt install sakura 小当然是他的最大优点了,虽小但是功能挺全 可以同时打开好多个终端,termin ...
- 网DAI之家简单爬取
用requests和bs做个简单的爬取网DAI之家的例子. 只做笔记用. #!/usr/bin/python3 import requestsfrom bs4 import BeautifulSoup ...
- java读取excel文件数据导入mysql数据库
这是我来公司的第二周的一个小学习任务,下面是实现过程: 1.建立maven工程(方便管理jar包) 在pom.xml导入 jxl,mysql-connector 依赖 可以在maven仓库搜索 2.建 ...
- java一周学习回顾
快速阅读 本周在学习java过程中主要是快马观花,对java的常用框架进行相关配置 ,进行简单的调用 .包括kafka,dubbo ,zookeeper.centos配置java环境.如何打war ...
- 个人学习分布式专题(二)分布式服务治理之分布式协调技术Zookeeper
分布式协调技术Zookeeper 2.1 zookeeper集群安装部署(略) 2.2 zookeeper的基本原理,数据模型 2.3 zookeeper Java api的使用 2.4 zookee ...
- bind--dns-docker---[nslookup/dig]
[dig] https://www.cnblogs.com/apexchu/p/6790241.html [dns resolution and revserse ]https://www.cnbl ...
- Matrix: 利用Matrix来设置ImageView的宽高,使图片能正常显示
在Android中加载ImageView一般都不会给ImageView的宽高设置一个确切的值,一般都是直接写成: <ImageView android:id="@+id/iv_test ...
- 关于Mysql-unknow-column-in-where-clause
写在前边: 已经很久不更新了啊,整个2月份几乎没有遇到什么新鲜事.直到昨天我又犯了一次傻,貌似只有我犯傻的时候才有材料可以跟大家分享- 问题表现: mysql 报错: unknow column 's ...
- 33Flutter仿京东商城项目 登录 注册相关页面布局
加群452892873 下载对应33课文件,运行方法,建好项目,直接替换lib目录 以下列出的是本课涉及的文件. User.dart import 'package:flutter/material. ...
- trylock方法
synchronized 是不占用到手不罢休的,会一直试图占用下去. 与 synchronized 的钻牛角尖不一样,Lock接口还提供了一个trylock方法.trylock会在指定时间范围内试图占 ...