二叉树的前序遍历 
 

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

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

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. 走进__proto__属性,看ie是否支持它,谁又来给他归宿

    每一个引用类型的实例中,都有一个指针,指向其原型对象.这个指针在非IE浏览器里通过__proto__表示,而在IE里不提供. 看如下代码: obj = {}; obj.__proto__.toStri ...

  2. TCP Congestion Control

    TCP Congestion Control Congestion occurs when total arrival rate from all packet flows exceeds R ove ...

  3. 极光推送能获取 registrationId,但是接收不到通知 - iOS

    集成极光推送进行调试的时候,运行 App 可以正常获取 registrationId,但是却迟迟无法收到推送消息,而Android 端是可以正常收到消息; 检查了证书配置和极光的配置一切正常,便开始返 ...

  4. github常见操作和常见错误

    配置git的时候会使用git config,三种配置分别为git config.git config --global.git config --system. 它们之前的优先级为(由高到低):git ...

  5. SQLMAP注入常见用法

    1.检查注入点 sqlmap -u http://www.com.tw/star_photo.php?artist_id=11 2.列数据库信息当前用户和数据库 sqlmap -u http://ww ...

  6. netstat命令的用法

    netstat用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况.利用netstat指令可让你得知整个Linux系统的网络情况.参数:-a或–all 显示 ...

  7. java 整型数据转换为小数类型 BigDecimal 装换为Double

    A,B为String类型 ,A-B=C BigDecimal A=(BigDecimal) map.get("A"); BigDecimal B=(BigDecimal) map. ...

  8. PHP运行原理之Opcodes

    在我之前的博客<Laravel5框架性能优化技巧>中提到开启OPcache可以提升php性能.那么为什么开启OPcache就可以提升php运行性能呢?这里就要提到php的运行原理了--Op ...

  9. yii学习笔记(5),视图操作

    在控制器调用$this->render()方法来输出视图 function actionLogin(){ $name = "admin"; // 加载视图 return $t ...

  10. java枚举常见用法

    用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法. p ...