树的先序遍历。定义一个栈,先压入中间结点并访问,然后依次压入右、左结点并访问。

 vector<int> preorderTraversal(TreeNode *root)
{
vector<int> result;
stack<TreeNode *>s;
TreeNode *p;
p = root;
s.push(p);
while (!s.empty())
{
p = s.top();
result.push_back(p->val);
if (p->right != nullptr)s.push(p->right);
if (p->left != nullptr)s.push(p->left);
} return result;
}

Leetcode 之Binary Tree Preorder Traversal(42)的更多相关文章

  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. [POI2014]FAR-FarmCraft 树形DP + 贪心思想

    (感觉洛谷上题面那一小段中文根本看不懂啊,好多条件都没讲,直接就是安装也要一个时间啊,,,明明不止啊!还好有百度翻译......) 题意:一棵树,一开始在1号节点(root),边权都为1,每个点有点权 ...

  2. 以安装PyTorch为例说明Anaconda在Windows/Linux上的使用

    在Windows10上配置完MXNet 1.3.0后,再配置PyTorch 1.0时,发现两者需要依赖的NumPy版本不一致,之前是通过pip安装NumPy,根据pip的版本不同,会安装不同版本的Nu ...

  3. BZOJ2761 不重复的数字 【treap】

    2761: [JLOI2011]不重复数字 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 5517  Solved: 2087 [Submit][S ...

  4. BZOJ1269 [AHOI2006]文本编辑器editor 【82行splay】

    1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4633  Solved: 1782 [Sub ...

  5. odex文件格式

    apk安装或启动时,会通过dexopt来将dex生成优化后的odex文件.过程是将apk中的classes.dex解压后,用dexopt处理并保存为“/data/dalvik-cache/data@a ...

  6. bzoj 1221 [HNOI2001] 软件开发 费用流

    [HNOI2001] 软件开发 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1938  Solved: 1118[Submit][Status][D ...

  7. POJ 3579 二分

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7687   Accepted: 2637 Descriptio ...

  8. HDU1711 KMP(模板题)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. JAVA、android中常用的一些jar包的作用

    正文: 这里主要介绍的是hibernate使用到的.jar Hibernate一共包括了23个jar包,令人眼花缭乱.本文将详细讲解Hibernate每个jar包的作用,便于你在应用中根据自己的需要进 ...

  10. 再续前缘-apache.commons.beanutils的补充

    title: 再续前缘-apache.commons.beanutils的补充 toc: true date: 2016-05-32 02:29:32 categories: 实在技巧 tags: 插 ...