LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++
Given a binary tree, return the preorder traversal of its nodes' values.
Example:
Input: [,null,,] \ / Output: [,,]
Follow up: Recursive solution is trivial, could you do it iteratively?
方法一:使用迭代(C++)
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res={};
if(!root)
return res;
stack<TreeNode*> s;
TreeNode* cur=root;
while(!s.empty()||cur){
while(cur){
res.push_back(cur->val);
s.push(cur);
cur=cur->left;
}
cur=s.top();
s.pop();
cur=cur->right;
}
return res;
}
方法二:使用递归,更简单,但是效率较低
void preOrder(TreeNode* root,vector<int>& m){
if(!root)
return;
m.push_back(root->val);
preOrder(root->left,m);
preOrder(root->right,m);
} vector<int> preorderTraversal(TreeNode* root) {
vector<int> res={};
if(!root)
return res;
preOrder(root,res);
return res;
}
LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++的更多相关文章
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- 【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; ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- lintcode :Binary Tree Preorder Traversal 二叉树的前序遍历
题目: 二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. 挑战 你能使用非递归实现么? 解题: 通过递 ...
- Leetcode 144 Binary Tree Preorder Traversal 二叉树
二叉树的基础操作:二叉树的先序遍历(详细请看数据结构和算法,任意本书都有介绍),即根,左子树,右子树,实现方法中还有用栈实现的,这里不介绍了 /** * Definition for binary t ...
- [LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历
关于二叉树的遍历请看: http://www.cnblogs.com/stAr-1/p/7058262.html /* 考察基本功的一道题,迭代实现二叉树前序遍历 */ public List< ...
- 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)
题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...
随机推荐
- Python练习四
1.任意输入一串文字加数字,统计出数字的个数,数字相连的视为一个,如:12fd2表示两个数字,即12为一个数字. content = input("请输入内容:") for i i ...
- admin-2
Linux是一种服务器操作系统 操作系统:一堆软件的集合,可以让计算机硬件正常工作 • UNIX诞生,1970-1-1(Linux系统时间的起点) • Linux之父,Linus Torwalds 内 ...
- js实现复制内容到粘贴板
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- smart contract 知识点
知识点 memory vs storage vs stack storage , where all the contract state variables reside. Every contra ...
- Devexpress之LayoutControl的使用及其控件布局设计
引言 Devexpress给我们提供了更加美观.更加丰富控件,但在学习和使用的同时经常会遇到诸多麻烦.今天在使用Devexpress的LayoutControl控件进行界面控件的布局设计时遇到了如下的 ...
- bond-vlan-bridge
拓扑介绍 Eth-Trunk5 down down 0% 0% 0 0 10GE1/0/5 down down 0.01% 0.01% 0 0 10GE2/0/5 down down 0.01% 0% ...
- Mysql--基础(一)
MySQL基础 一.数据库的操作 1.SQL分类: DDL(数据定义语言) :数据定义语言 - Data Definition Language,用来定义数据库的对象,如数据表.视图.索引等.常用 ...
- Vue 错误记录:Cannot read property 'beforeRouteEnter' of undefined
点击某路由链接,页面提示: Cannot read property 'beforeRouteEnter' of undefined 查看代码并无手写beforeRouterEnter设置, 把页面内 ...
- 【DevExpress】GridView的RowClick事件禁用Checkbox选择的解决办法
添加GridView的RowCellClick事件,代码如下 private void gvBoxMails_RowCellClick(object sender, DevExpress.XtraGr ...
- (C#)生成指定长度的随机字符串的通用方法
.NET(C#)生成指定长度的随机字符串的通用方法,此方法可以指定字符串的长度,是否包含数字,是否包含符号,是否包含小写字母,是否包含大写字母等, 源码: #region 生成指定长度的随机字符串 / ...