二叉树的基础操作:二叉树的先序遍历(详细请看数据结构和算法,任意本书都有介绍),即根,左子树,右子树,实现方法中还有用栈实现的,这里不介绍了

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void dfs(vector<int> &ans,TreeNode *root){//二叉树的先序遍历
if(!root){
return;
}
ans.push_back(root->val);
dfs(ans,root->left);
dfs(ans,root->right);
}
vector<int> preorderTraversal(TreeNode *root) {
vector<int> ans;
dfs(ans,root);
return ans;
}
};

Leetcode 144 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 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...

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

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

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

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

  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】Binary Tree Preorder Traversal(二叉树的前序遍历)

    这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...

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

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

  9. Java [Leetcode 144]Binary Tree Preorder Traversal

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

随机推荐

  1. Diskpart工具应用两则:MBR/GPT分区转换 &amp; 基本/动态磁盘转换

    将基本磁盘转换为动态磁盘可直接在操作系统的磁盘管理中完毕,如图1所看到的,这一转换过程对硬盘上的数据没有影响,可是可能会影响到系统的启动(盗版系统激活会受影响). 图1:基本磁盘转换为动态磁盘 要注意 ...

  2. Android ViewGroup使用小结

    ViewGroup定义 在api中是这么描写叙述ViewGroup的:A ViewGroup is a special view that can contain other views. 依据意思我 ...

  3. WindowImplBase::OnSysCommand-------duilib在最大化和还原间切换

    virtual LRESULT OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { if( wPar ...

  4. 使用xerces库的一个注意事项

    作者:朱金灿 来源:http://blog.csdn.net/clever101 使用xerces库解析xml文件,结果出现这样一些链接错误: public: static classxercesc_ ...

  5. 【转】移动Web开发-点击事件及页面滚动

    点击事件 移动端浏览器点击事件默认有300ms的延迟 移动端实现弹性滚动 安卓局部滚动 滚动条出现bug,解决方案:Android只是用全局滚动 模拟全局滚动,加上padding-top及paddin ...

  6. 美轮美奂宇宙星空制作神器Spacescape

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/46444569 作者:car ...

  7. 使用Apache FtpServer搭建FTP服务器 [FlashFXP]

    <server xmlns="http://mina.apache.org/ftpserver/spring/v1" xmlns:xsi="http://www.w ...

  8. ccpc2016长春站打铁记(后记)

    Day3 "学术交流日" 自己进我的空间看吧. http://user.qzone.qq.com/190741511/4

  9. [GraphQL] Use GraphQLList with GraphQLObject Types

    When working with collections of things in GraphQL, we'll always reach out for the GraphQLListType. ...

  10. 【Record】9.16..9.23