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

 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. [HNOI2004]树的计数 prufer数列

    题面: 一个有n个结点的树,设它的结点分别为v1, v2, …, vn,已知第i个结点vi的度数为di,问满足这样的条件的不同的树有多少棵.给定n,d1, d2, …, dn,你的程序需要输出满足d( ...

  2. POJ1061:青蛙的约会——题解

    http://poj.org/problem?id=1061 Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定 ...

  3. 函数strcat实现

    1.函数原型 extern char *strcat(char *dest,const char *src); 注:在C++中,则存在于<cstring>头文件中. 2.函数功能: str ...

  4. 实验五 TCP传输及加解密

    北京电子科技学院(BESTI) 实     验    报     告 课程:Java程序设计                         班级:1353            姓名:陈巧然     ...

  5. NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第二轮Day2题解

    肝了两题... T1一眼题,分解质因数,找出2的个数和5的个数取min输出 #include<iostream> #include<cstring> #include<c ...

  6. JavaScript转换与解析JSON的方法

    在JavaScript中将JSON的字符串解析成JSON数据格式,一般有两种方式: 一种为使用eval()函数. 使用Function对象来进行返回解析. 使用eval函数来解析,jquery的eac ...

  7. POJ3974:Palindrome(Manacher模板)

    Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 14021   Accepted: 5374 题目链接 ...

  8. Java Zip压缩

    1.压缩文件或整个目录 // ZipCompression.java import java.io.File;import java.io.FileInputStream;import java.io ...

  9. linux查找进程id端口占用和杀死进程

    linux 查找进程id端口占用和杀死进程 ps 命令用于查看当前正在运行的进程 辅助上grep 用于搜索匹配ps -ef | grep java ps ax : 显示当前系统进程的列表 ps aux ...

  10. 51Nod 1090 3个数之和

    Input示例 7 -3 -2 -1 0 1 2 3 Output示例 -3 0 3 -3 1 2 -2 -1 3 -2 0 2 -1 0 1 #include "bits/stdc++.h ...