http://oj.leetcode.com/problems/binary-tree-preorder-traversal/

二叉树的先跟遍历,写的是递归版本,也可以使用stack来进行,替代了递归函数。

class Solution {
public:
void preOrder(TreeNode *root,vector<int> &ans)
{
ans.push_back(root->val);
if(root->left)
preOrder(root->left,ans);
if(root->right)
preOrder(root->right,ans);
}
vector<int> preorderTraversal(TreeNode *root) {
vector<int> ans;
if(root)
preOrder(root,ans);
return ans;
}
};

LeetCode OJ--Binary Tree Preorder Traversal的更多相关文章

  1. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  2. [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历

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

  3. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  4. [LeetCode 题解]: Binary Tree Preorder Traversal

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a bi ...

  5. 【leetcode】Binary Tree Preorder Traversal (middle)★

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

  6. Java for LeetCode 144 Binary Tree Preorder Traversal

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

  7. leetcode 144. Binary Tree Preorder Traversal ----- java

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

  8. leetcode 题解:Binary Tree Preorder Traversal (二叉树的先序遍历)

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

  9. Java [Leetcode 144]Binary Tree Preorder Traversal

    题目描述: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given bin ...

  10. (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...

随机推荐

  1. pandas关联mysql并读写数据库

    1.代码读写mysql,必须安装关联mysql的工具 操作如下命令: sudo apt-get install mysql-server mysql-clientsudo apt-get instal ...

  2. Docker DockerFile文件指令 & 构建

    1.dockerfile指令格式 # Comment注释 INSTRUCTION argument指令名 + 参数 2.普通指令 1. FROM 已存在的镜像,基础镜像,第一条非注释指令 FROM & ...

  3. C++图书馆管理系统项目中部分功能代码实现(书籍推荐)

    bool UserServiceImpl::Compare1(Book b1,Book b2)//按照借阅次数比较{ if(b1.GetCnt() > b2.GetCnt()) { return ...

  4. vue组件:canvas实现图片涂鸦功能

    方案背景 需求 需要对图片进行标注,导出图片. 需要标注N多图片最后同时保存. 需要根据多边形区域数据(区域.颜色.名称)标注. 对应方案 用canvas实现涂鸦.圆形.矩形的绘制,最终生成图片bas ...

  5. Install GStreamer on Ubuntu

    apt-get install libgstreamer1.0-0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-p ...

  6. POJ:2411-Mondriaan's Dream(矩形拼接方案)

    题目链接:http://poj.org/problem?id=2411 解题心得: 可以说是很经典的一个状压dp了,写dfs遍历肯定是要超时的,这个题的状态转移方程对新手来说有点吃力. 状态转移用的是 ...

  7. 深入Python底层,谈谈内存管理机制

    说到内存管理,就先说一下垃圾回收吧.垃圾回收是Python,Java等语言管理内存的一种方式,说的直白些,就是清除无用的垃圾对象.C语言及C++中,需要通过malloc来进行内存的申请,通过free而 ...

  8. react技术栈实践(2)

    本文来自网易云社区 作者:汪洋 这时候还没完,又有两个问题引出来了. 按照上面的配置,第三方库 antd 竟然也被编译了,导致样式失败. react中,一旦包裹了子组件,子组件没办法直接使用 styl ...

  9. TCP 中的三次握手和四次挥手

    Table of Contents 前言 数据报头部 三次握手 SYN 攻击 四次挥手 半连接 TIME_WAIT 结语 参考链接 前言 TCP 中的三次握手和四次挥手应该是非常著名的两个问题了,一方 ...

  10. configparser模块——用于生成和修改常见配置文档

    配置文档格式 [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [b ...