二叉树的前序遍历 
 

给出一棵二叉树,返回其节点值的前序遍历。

您在真实的面试中是否遇到过这个题?

Yes
样例

给出一棵二叉树 {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 二叉树前序遍历的更多相关文章

  1. [LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历

    关于二叉树的遍历请看: http://www.cnblogs.com/stAr-1/p/7058262.html /* 考察基本功的一道题,迭代实现二叉树前序遍历 */ public List< ...

  2. [leetcode/lintcode 题解] 前序遍历和中序遍历树构造二叉树

    [题目描述] 根据前序遍历和中序遍历树构造二叉树. 在线评测地址: https://www.jiuzhang.com/solution/construct-binary-tree-from-preor ...

  3. lintcode.66 二叉树前序遍历

    二叉树的前序遍历    描述 笔记 数据 评测 给出一棵二叉树,返回其节点值的前序遍历. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返 ...

  4. LintCode_69 二叉树前序遍历

    题目 给出一棵二叉树,返回其节点值的前序遍历. 和中序遍历基本相同 C++代码 vector<int> preorderTraversal(TreeNode *root) { // wri ...

  5. binTreepreorderTraversal二叉树前序遍历

    原题 Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binar ...

  6. [Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal

    [题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null ...

  7. LintCode 二叉树的遍历 (非递归)

    前序: class Solution { public: /** * @param root: The root of binary tree. * @return: Preorder in vect ...

  8. 144. Binary Tree Preorder Traversal (二叉树前序遍历)

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  9. lintcode :前序遍历和中序遍历树构造二叉树

    解题 前序遍历和中序遍历树构造二叉树 根据前序遍历和中序遍历树构造二叉树. 样例 给出中序遍历:[1,2,3]和前序遍历:[2,1,3]. 返回如下的树: 2 / \ 1 3 注意 你可以假设树中不存 ...

随机推荐

  1. css圆角和阴影兼容问题(ie7,ie8)

    <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...

  2. win8安装wampserver报403错误解决方法

    看着别人开始体验win8了,前几天我也安装了win8系统,总体来说还不错,但是今天安装完Wampserver后,浏览器输入localhost,竟然报了403错误,我以为我安装出错了,后来研究了半天,发 ...

  3. Gradle Goodness: Adding Tasks to a Predefined Group

    In Gradle we can group related tasks using the group property of a task. We provide the name of our ...

  4. 记一次数据库同步经历(sql server 2008)

    前阵子搞了下数据库同步,大概意思就是服务器上有一个数据库,与本地数据库进行同步,服务器上的数据库有什么改变,可以同步到本地数据库中.做之前百度了下,流程分以下三步, 第一步: 服务器上的数据库进行发布 ...

  5. .net core运行环境搭建 linux + windows

    ---------------------------------------linux------------------------------------------------- 一.添加do ...

  6. 自定义UICollectionViewLayout并添加UIDynamic

    大家也可以到这里查看. UICollectionView是iOS6引入的控件,而UIDynamicAnimator是iOS7上新添加的框架.本文主要涵盖3部分: 一是简单概括UICollectionV ...

  7. iOS- CoreData 数据库管理利器!

    1.前文 上次用SQLite3实现了数据管理,这次准备用CoreData来实现. Core Data 是iOS SDK 里的一个很强大的框架,允许程序员以面向对象的方式储存和管理数据.使用Core D ...

  8. 破损的键盘(悲剧文本)(Broken Keyboard(a.k.a. Beiju Text),Uva 11988)

    破损的键盘(悲剧文本)(Broken Keyboard(a.k.a. Beiju Text),Uva 11988) 题意描述 你在输入文章的时候,键盘上的Home键和End键出了问题,会不定时的按下. ...

  9. python中的extend和append

    list.append(object) 向列表中添加一个对象object list.extend(sequence)  把一个序列seq的内容添加到列表中 old = ['a', 'b'] new = ...

  10. C#获取本地磁盘信息

    直接上干货简单易懂 //磁盘监控(远程/本地) //需要引用System.Management.dll public class RemoteMonitoring { private static s ...