LeetCode OJ--Binary Tree Preorder Traversal
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的更多相关文章
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- [LeetCode 题解]: Binary Tree Preorder Traversal
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a bi ...
- 【leetcode】Binary Tree Preorder Traversal (middle)★
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 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 ...
- leetcode 144. Binary Tree Preorder Traversal ----- java
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- leetcode 题解:Binary Tree Preorder Traversal (二叉树的先序遍历)
题目: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binar ...
- Java [Leetcode 144]Binary Tree Preorder Traversal
题目描述: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given bin ...
- (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
随机推荐
- Python学习笔记:configparser(INI格式配置文件解析)
在平时的开发中感觉INI格式的配置文件使用还是挺需要的,有时会使用一个单独的py来存放一些常量或者配置项,大多时候这样倒是挺好用的,但是如果某些配置项需要在运行时由用户来修改指定,比如很多app在关闭 ...
- python3.7 文件操作
#!/usr/bin/env python __author__ = "lrtao2010" #python3.7 文件操作 # r 只读,默认打开方式,当文件不存在时会报错 # ...
- 03 Django视图
功能 接受Web请求HttpRequest,进行逻辑处理,与 M 和 T 进行交互,返回 Web 响应 HttpResponse 给请求者 示例项目的创建 创建项目 test3 django-admi ...
- debian软raid
http://www.linuxidc.com/Linux/2013-06/86487.htm
- HTTP认证之摘要认证——Digest(二)
导航 HTTP认证之基本认证--Basic(一) HTTP认证之基本认证--Basic(二) HTTP认证之摘要认证--Digest(一) HTTP认证之摘要认证--Digest(二) 在HTTP认证 ...
- build.xml: 21: Class not found: javac1.8
在eclipse里运用ant时经常碰到class not found的错误提示,从而编译失败,其实是eclipse中本身的ant版本太老造成该的,但我今天安装的ant是1.8.4,感觉已经很新了,但编 ...
- Mysql登陆、退出、更改环境编码
登录: mysql -h[数据库地址] -u[username] -p[password] -P[端口] //大写P表示端口,小写p表示密码 例如:mysql -hlocalhost -uroot ...
- dubbo doc入门文档
dubbo document http://dubbo.apache.org/zh-cn/docs/user/references/xml/dubbo-protocol.html
- loj2012 「SCOI2016」背单词
-- #include <algorithm> #include <iostream> #include <cstring> #include <cstdio ...
- 1.部署虚拟环境安装linux系统
第1章 部署虚拟环境安装linux系统 章节简述: 本章从零基础详细讲解了虚拟机软件与红帽Linux系统,完整演示了VM虚拟机的安装与配置过程,以及红帽RHEL 7系统的安装.配置过程和初始化方法.此 ...