Leetcode144. Binary Tree Preorder Traversal二叉树的前序遍历
给定一个二叉树,返回它的 前序 遍历。
示例:
输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
递归:
class Solution {
public:
vector<int> res;
vector<int> preorderTraversal(TreeNode* root)
{
GetAns(root);
return res;
}
void GetAns(TreeNode* root)
{
if(root == NULL)
return;
res.push_back(root ->val);
GetAns(root ->left);
GetAns(root ->right);
}
};
迭代:
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root)
{
vector<int> res;
if(root == NULL)
return res;
stack<TreeNode*> s;
TreeNode *cur = root;
while(!s.empty() || cur)
{
if(cur != NULL)
{
res.push_back(cur ->val);
if(cur ->right != NULL)
s.push(cur ->right);
cur = cur ->left;
}
else
{
cur = s.top();
s.pop();
}
}
return res;
}
};
Leetcode144. Binary Tree Preorder Traversal二叉树的前序遍历的更多相关文章
- lintcode :Binary Tree Preorder Traversal 二叉树的前序遍历
题目: 二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. 挑战 你能使用非递归实现么? 解题: 通过递 ...
- LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...
- 【LeetCode】Binary Tree Preorder Traversal(二叉树的前序遍历)
这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...
- 144 Binary Tree Preorder Traversal 二叉树的前序遍历
给定一棵二叉树,返回其节点值的前序遍历.例如:给定二叉树[1,null,2,3], 1 \ 2 / 3返回 [1,2,3].注意: 递归方法很简单,你可以使用迭代方法来解决 ...
- LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium
题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...
- 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(二叉树先序遍历Medium)
题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...
- [LeetCode] 144. 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 ...
随机推荐
- 2_1.springboot2.x配置之配置文件解析
1.配置文件 1.Spring Boot使用一个全局的配置文件:•application.properties.application.yml 2.配置文件放在src/main/resources目录 ...
- unity3d入门 Demo 学习记录
闲来学习一下 unity3d 的Demo,记录如下. 官方 Demo,名字为 Roll-A-Ball,如图 场景比较简单,包含地面.玩家精灵.主摄像机.墙壁.可拾取的方块.分数为示 text.平行光源 ...
- Oracle Database 18c数据库安装步骤
1.Oracle官网登录下载https://login.oracle.com/mysso/signon.jsp WINDOWS.X64_180000_db_home.zip 2.D盘根目录新建文件夹: ...
- SoapUI测试接口【转】
下载安装soapUI工具,具体安装按照提示往下走就可以,这里不着重说明,下面是我打开soapUI工具的起始窗口: 在Projects上鼠标右键点击,选择new soap project(新建一个SO ...
- C++ AOP手法
1.代理模式 2.模版 3.NVI(non-virtual interface) 参考:https://www.cnblogs.com/qicosmos/p/4772389.html <effe ...
- 【转载】TCP演进简述
TCP演进简述 http://www.cnblogs.com/fll/ 一.互联网概述 TCP,即传输控制协议,是目前网络上使用的最多的传输协议,我们知道,整个互联网的体系结构是以IP协议提供的无连接 ...
- Ubuntu 12.04 Eclipse设置 Javadoc背景色
在Ambiance主题下,eclipse弹出的tip是黑色背景的,这样压根就看不清java doc. 当然可以在外观改变系统主题为其他主题,相应的gtk-2.0/gtkrc要重新设置,比如Ubuntu ...
- momentjs 使用总结
一.安装 cnpm install moment 二.引入 var moment = require('moment') 三.使用 let today = moment().format('YYYY- ...
- PAT甲级——A1097 Deduplication on a Linked List
Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...
- java基础温习 -- Thread synchronized关键字
synchronized 基本规则 1. 当一个线程访问“某对象”的“synchronized方法”或者“synchronized代码块”时,其他线程对“该对象”的该“synchronized方法”或 ...