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

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

  1. class Solution {
  2. public:
  3. void preOrder(TreeNode *root,vector<int> &ans)
  4. {
  5. ans.push_back(root->val);
  6. if(root->left)
  7. preOrder(root->left,ans);
  8. if(root->right)
  9. preOrder(root->right,ans);
  10. }
  11. vector<int> preorderTraversal(TreeNode *root) {
  12. vector<int> ans;
  13. if(root)
  14. preOrder(root,ans);
  15. return ans;
  16. }
  17. };

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. Python学习笔记:configparser(INI格式配置文件解析)

    在平时的开发中感觉INI格式的配置文件使用还是挺需要的,有时会使用一个单独的py来存放一些常量或者配置项,大多时候这样倒是挺好用的,但是如果某些配置项需要在运行时由用户来修改指定,比如很多app在关闭 ...

  2. python3.7 文件操作

    #!/usr/bin/env python __author__ = "lrtao2010" #python3.7 文件操作 # r 只读,默认打开方式,当文件不存在时会报错 # ...

  3. 03 Django视图

    功能 接受Web请求HttpRequest,进行逻辑处理,与 M 和 T 进行交互,返回 Web 响应 HttpResponse 给请求者 示例项目的创建 创建项目 test3 django-admi ...

  4. debian软raid

    http://www.linuxidc.com/Linux/2013-06/86487.htm  

  5. HTTP认证之摘要认证——Digest(二)

    导航 HTTP认证之基本认证--Basic(一) HTTP认证之基本认证--Basic(二) HTTP认证之摘要认证--Digest(一) HTTP认证之摘要认证--Digest(二) 在HTTP认证 ...

  6. build.xml: 21: Class not found: javac1.8

    在eclipse里运用ant时经常碰到class not found的错误提示,从而编译失败,其实是eclipse中本身的ant版本太老造成该的,但我今天安装的ant是1.8.4,感觉已经很新了,但编 ...

  7. Mysql登陆、退出、更改环境编码

    登录: mysql -h[数据库地址] -u[username] -p[password] -P[端口]  //大写P表示端口,小写p表示密码 例如:mysql -hlocalhost -uroot ...

  8. dubbo doc入门文档

    dubbo document http://dubbo.apache.org/zh-cn/docs/user/references/xml/dubbo-protocol.html

  9. loj2012 「SCOI2016」背单词

    -- #include <algorithm> #include <iostream> #include <cstring> #include <cstdio ...

  10. 1.部署虚拟环境安装linux系统

    第1章 部署虚拟环境安装linux系统 章节简述: 本章从零基础详细讲解了虚拟机软件与红帽Linux系统,完整演示了VM虚拟机的安装与配置过程,以及红帽RHEL 7系统的安装.配置过程和初始化方法.此 ...