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

 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. YY的GCD 莫比乌斯反演

    ---题面--- 题解: $ans = \sum_{x = 1}^{n}\sum_{y = 1}^{m}\sum_{i = 1}^{k}[gcd(x, y) == p_{i}]$其中k为质数个数 $$ ...

  2. cf 443 D. Teams Formation](细节模拟题)

    cf 443 D. Teams Formation(细节模拟题) 题意: 给出一个长为\(n\)的序列,重复\(m\)次形成一个新的序列,动态消除所有k个连续相同的数字,问最后会剩下多少个数(题目保证 ...

  3. css命名冲突解决方法

    css的命名冲突目前有几种解决方法: 1.命名约定 人为的制定一下命名规则以避免冲突,例如前缀,嵌套等 2.CSS in JS 在JavaScript中写CSS,使用工具编译为css,最常见的是sty ...

  4. cgroups 命令集

    cgroups 命令集 最后介绍,功能最为强大的控制组(cgroups)的用法.cgroups 是 Linux 内核提供的一种机制,利用它可以指定一组进程的资源分配. 具体来说,使用 cgroups, ...

  5. https客户端遇到过的问题

    1.用.p12格式的证书,在windows上调试完全没问题,在Linux服务器上,提示无效证书格式. 解决方法:将.p12格式的证书转换为.jks格式的证书. 将.p12格式的证书转换为.jks格式的 ...

  6. zabbix问题汇总

    1. ::110809.577 resuming IPMI checks on host [10.1.3.41]: connection restored :: seconds :: seconds ...

  7. Makefile中的 =,:=,?=,+= 的差异

    在Makefile中常常遇见这几种等操作,总结一下具体区别. =  是最基本的赋值 :=  是用右值覆盖左值 ?=  判断,如果左值没有被赋值过就赋以右值,否则,不做赋值动作 += 在左值后面连接右值 ...

  8. bzoj 1564 [NOI2009]二叉查找树 区间DP

    [NOI2009]二叉查找树 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 906  Solved: 630[Submit][Status][Discu ...

  9. EL表达式获取对象属性的原理

    EL表达式获取对象属性的原理是这样的: 以表达式${user.name}为例 EL表达式会根据name去User类里寻找这个name的get方法,此时会自动把name首字母大写并加上get前缀,一旦找 ...

  10. [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    洛谷题目链接:[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...