1. Binary Tree Preorder Traversal My Submissions QuestionEditorial Solution

    Total Accepted: 119655 Total Submissions: 300079 Difficulty: Medium

    Given a binary tree, return the preorder traversal of its nodes’ values.

For example:

Given binary tree {1,#,2,3},

1

\

2

/

3

return [1,2,3].

思路:用栈来进行先序遍历,先入栈的后遍历

时间复杂度:O(n)

空间复杂度:O(1)

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> vtn;
if(root==NULL)return vtn; //为空时先处理,不然后面死循环
stack<TreeNode*> stn;
stn.push(root);
while(!stn.empty()){
TreeNode *p = stn.top();
vtn.push_back(p->val);
stn.pop();
if(p->right!=NULL)stn.push(p->right);
if(p->left!=NULL)stn.push(p->left);
}
return vtn;
}
};

55. Binary Tree Preorder Traversal的更多相关文章

  1. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  2. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  3. 3月3日(3) Binary Tree Preorder Traversal

    原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的..  啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...

  4. Binary Tree Preorder Traversal on LeetCode in Java

    二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...

  5. Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  6. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  7. 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  8. LeetCode: Binary Tree Preorder Traversal 解题报告

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  9. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

随机推荐

  1. elasticsearch地理位置查询

    elasticsearch地理位置查询 一.背景 二.geo数据类型 1.geo_point 2.geo_shape 三.此处对geo_point类型实战 1.背景 2.插入地点数据 1.创建索引 2 ...

  2. 鸿蒙轻内核M核的故障管家:Fault异常处理

    摘要:本文先简单介绍下Fault异常类型,向量表及其代码,异常处理C语言程序,然后详细分析下异常处理汇编函数实现代码. 本文分享自华为云社区<鸿蒙轻内核M核源码分析系列十八 Fault异常处理& ...

  3. Get_init_color_map

    #!/bin/bash./simulate_screencap.sh./analysis_screencap.py

  4. Python学习路线【对标大厂Python工程师的招聘要求,并推荐优质免费资源】打卡学习不迷茫

    您好,我是码农飞哥,感谢您阅读本文,欢迎一键三连哦. 本文要点:从Python爬虫工程师的招聘要求出发制定学习路线,同时还推荐免费优质的学习资源. 打卡学习不迷茫. 干货满满,建议收藏,需要用到时常看 ...

  5. 装了这几个IDEA插件,基本上一站式开发了!

    前言 前几天有社区小伙伴私聊我,问我都用哪些IDEA插件,我的IDEA的主题看起来不错. 作为一个开源作者,每周要code大量的代码,提升日常工作效率是我一直追求的,在众多的IDEA插件中,我独钟爱这 ...

  6. VSCode C/C++ 开发环境配置 详细教程

    本博客已暂停更新,需要请转新博客http://www.whbwiki.com/335.html VsCode是一个轻量级的编辑器,但是配置有点复杂,这里我做了C/C++开发环境配置总结,适用于wind ...

  7. 【数据结构&算法】09-队列概念&参考源码

    目录 前言 队列的定义 队列的抽象数据类型 循环队列与链式队列对比 循环队列 特点 定义 循环队列相关计算 链式队列 定义 阻塞队列 并发队列 代码实现 循环队列代码 链式队列实现 前言 李柱明博客: ...

  8. Linux基础三:用户和组

    三.用户和组 1.概念 (1).用户概念: 用户是用来运行某一些进程.拥有某一些文件或目录. 在Linux里面,用户分成三大类:root用户.系统用户.普通用户. 用户是用UID来唯一标识身份的,且r ...

  9. 使用VSCode编写,发布cnblogs

    WriteCnBlog插件作者写的教程: https://www.cnblogs.com/caipeiyu/p/11774968.html

  10. Salesforce Consumer Goods Cloud 浅谈篇四之店内拜访的创建和执行

    本篇参考: https://v.qq.com/x/page/f0772toebhd.html https://v.qq.com/x/page/e0772tsmtek.html https://v.qq ...