lintcode 二叉树前序遍历
二叉树的前序遍历
给出一棵二叉树,返回其节点值的前序遍历。
给出一棵二叉树 {1,#,2,3},
1
\
2
/
3
返回 [1,2,3].
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/ //递归
class Solution {
public:
/*
* @param root: A Tree
* @return: Preorder in ArrayList which contains node values.
*/ void inorder (TreeNode *root, vector<int> &result) {
result.push_back(root->val);
if (root->left != NULL) {
inorder(root->left, result);
} if (root->right != NULL) {
inorder(root->right, result);
}
} vector<int> preorderTraversal(TreeNode * root) {
// write your code here
vector<int> result;
if (root == NULL) {
return result;
}
inorder(root, result);
return result;
}
};
lintcode 二叉树前序遍历的更多相关文章
- [LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历
关于二叉树的遍历请看: http://www.cnblogs.com/stAr-1/p/7058262.html /* 考察基本功的一道题,迭代实现二叉树前序遍历 */ public List< ...
- [leetcode/lintcode 题解] 前序遍历和中序遍历树构造二叉树
[题目描述] 根据前序遍历和中序遍历树构造二叉树. 在线评测地址: https://www.jiuzhang.com/solution/construct-binary-tree-from-preor ...
- lintcode.66 二叉树前序遍历
二叉树的前序遍历 描述 笔记 数据 评测 给出一棵二叉树,返回其节点值的前序遍历. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返 ...
- LintCode_69 二叉树前序遍历
题目 给出一棵二叉树,返回其节点值的前序遍历. 和中序遍历基本相同 C++代码 vector<int> preorderTraversal(TreeNode *root) { // wri ...
- binTreepreorderTraversal二叉树前序遍历
原题 Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binar ...
- [Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal
[题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null ...
- LintCode 二叉树的遍历 (非递归)
前序: class Solution { public: /** * @param root: The root of binary tree. * @return: Preorder in vect ...
- 144. Binary Tree Preorder Traversal (二叉树前序遍历)
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- lintcode :前序遍历和中序遍历树构造二叉树
解题 前序遍历和中序遍历树构造二叉树 根据前序遍历和中序遍历树构造二叉树. 样例 给出中序遍历:[1,2,3]和前序遍历:[2,1,3]. 返回如下的树: 2 / \ 1 3 注意 你可以假设树中不存 ...
随机推荐
- tensorflow节点布放(device assignment of node)算法:simpler_placer
tensorflow v0.9中目前在用的devcie assignment算法是simple placer算法,相比于白皮书中cost model算法实现简单.simpler placer算法优先选 ...
- 微信小程序获取手机信息
wx.getSystemInfo({ success: function (res) { console.log(res.model)//手机型号 console.log(res.pixelRatio ...
- OpenID Connect Core 1.0(三)验证
OpenID Connect执行终端用户登录或确定终端用户已经登录的验证工作.OpenID Connect 使服务器以一种安全的方式返回验证结果.所以客户可以依靠它.出于这个原因,在这种情况下客户被称 ...
- Knowledge Point 20180305 详解精度问题
1.1 精度与基本数据类型运算的深度解析 我们在探讨Java基本数据类型时多次提到过精度的问题,那么计算机中的精度究竟是什么样的,为什么我们有时候的计算和我们预期的不同呢?下面我们通过精度来了解: 1 ...
- mysqld_safe之三言两语
today,one buddy in IMG wechat group 2 asked "why i've installed the MySQL 5.7 on linux serv ...
- ASP.NET Core获取微信订单数据
前几天对接了一波微信的订单,分享出来 1.生成签名 根据微信要求把appid.商户号.随机数.和订单号还有商户平台的密钥拼接成一个字符串然后进行MD5加密 2.拼接请求XML 然后用拼接好的XML向微 ...
- Xshell配色方案推荐
使用方法: 新建mycolor.xcs文件 复制粘贴如下代码,将文件导入,修改自己喜欢的字体即可 [mycolor] text=00ff80 cyan(bold)=00ffff text(bold)= ...
- MySQL的JOIN用法
JOIN的含义就如英文单词“join”一样,连接两张表,大致分为内连接,外连接,右连接,左连接,自然连接.这里描述先甩出一张用烂了的图,然后插入测试数据. CREATE TABLE t_blog( i ...
- array of TVarRec 动态数组使用
FDQuery.AppendRecord()里是一个array of TVarRec.我们一般都是直接用[Var1,Var2,...].这样手工输入,但如果增加的元素我们预先不知道,就要声明一个arr ...
- Linux下Bash shell学习笔记
原文地址: http://www.cnblogs.com/NickQ/p/8870423.html 1.shell下没有变量类型和定义的概念. 变量直接使用不用定义 所有值都视为字符串. 在对变量取值 ...