【LeetCode 144_二叉树_遍历】Binary Tree Preorder Traversal

解法一:非递归
vector<int> preorderTraversal(TreeNode* root)
{
vector<int> res;
if (root == NULL)
return res; stack<TreeNode*> node_stack;
TreeNode *p = root;
while (p || !node_stack.empty()) {
if (p) {
res.push_back(p->val);
node_stack.push(p);
p = p->left;
} else {
p = node_stack.top();
node_stack.pop();
p = p->right;
}
}
return res;
}
解法二:递归
void preorderTraversalSub(TreeNode* root, vector<int> &res)
{
if (root == NULL)
return;
res.push_back(root->val);
preorderTraversalSub(root->left, res);
preorderTraversalSub(root->right, res);
} vector<int> preorderTraversal(TreeNode* root)
{
vector<int> res;
preorderTraversalSub(root, res);
return res;
}
【LeetCode 144_二叉树_遍历】Binary Tree Preorder Traversal的更多相关文章
- [Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal
[题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null ...
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...
- [Swift]LeetCode144. 二叉树的前序遍历 | Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
- 【Leetcode】【Medium】Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【leetcode刷题笔记】Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- Binary Tree Preorder Traversal on LeetCode in Java
二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
随机推荐
- 爬虫之selenium使用
详细使用链接: 点击链接 selenium介绍: selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质 ...
- MDF文件损坏,如何恢复?(未解决)
MDF文件损坏,如何恢复?MDF附加失败,数据库附加失败 1.附加时 2.用替换法设置后重建日志 (其实已经删掉日志了,确保操作之前没有日志,但是运行 alter database [test] Re ...
- C# Winform backgroundWorker组件使用
BackgroundWorker 组件用来执行诸如数据库事务.文件下载等耗时的异步操作. 开始 在应用程序中添加一个BackgroundWorker实例,如果用的是VS,可以从工具上直接拖到应用程序: ...
- Java基础知识陷阱(九)
本文发表于本人博客. 今天我来说说关于JAVA多线程知识,有错误请指出.大家都知道JAVA在服务端上处理也有很大优势,很多公司也有在服务器跑JAVA进程,这说明JAVA在处理这个多线程以及并发下也有一 ...
- VS2010/MFC编程入门之九(对话框:为控件添加消息处理函数)
创建对话框类和添加控件变量在上一讲中已经讲过,这一讲的主要内容是如何为控件添加消息处理函数. MFC为对话框和控件等定义了诸多消息,我们对它们操作时会触发消息,这些消息最终由消息处理函数处理.比如我们 ...
- 528. Random Pick with Weight
1. 问题 给定一个权重数组w,w[i]表示下标i的权重,根据权重从数组中随机抽取下标. 2. 思路 这道题相当于 497. Random Point in Non-overlapping Recta ...
- Databases Questions & Answers
Databases Questions & Answers 1. What are two methods of retrieving SQL? 2. What curso ...
- mybatis的namespace
Mybatis的namespace是用来绑定Dao接口的,使用了namespace之后就可以不用写接口实现类,dao接口的方法对应mapper.xml中的sql语句. 详情见:https://blog ...
- 初识PHP(二)常用函数
在此记录一些常用库函数和常用语法以便查阅 一.PHP手册 php手册中文地址 http://php.net/manual/zh 二.一些常用操作 2.1字符串操作 2.1.1 strpos — 查找字 ...
- CEF禁止右键菜单
转载:http://www.cctry.com/thread-258549-1-1.html 转载:http://blog.sina.com.cn/s/blog_dad2c54101019cmo.ht ...