【LeetCode】树的遍历
非递归中序遍历:
思路:注释
vector<int> inorderTraversal(TreeNode* root) {
vector<int>ret;
if(root == NULL) return ret;
stack<TreeNode*>sta;
TreeNode *p = root;
while(!sta.empty() || p != NULL){
// 当前节点不空 就一直左扎进
while(p != NULL){
sta.push(p);
p = p -> left;
}
// 无左孩子 处理根节点 然后右孩子如果右空电话,p = NULL 下一次直接处理其父结点
if(!sta.empty()){
p = sta.top();
sta.pop();
cout<<p->val<<endl;
ret.push_back(p->val);
p = p -> right;
} }
return ret;
}
非递归前序遍历:
vector<int> preorderTraversal(TreeNode* root) {
vector<int>ret;
if(!root) return ret;
stack<TreeNode *>s;
TreeNode *p = root;
while(p != NULL || !s.empty()){
while(p){
cout<< p->val <<endl;
ret.push_back(p->val);
s.push(p);
p = p -> left;
}
if(!s.empty()){
p = s.top();
s.pop();
p = p -> right;
}
}
return ret;
}
【LeetCode】树的遍历的更多相关文章
- LeetCode总结 -- 树的遍历篇
遍历树的数据结构中最常见的操作. 能够说大部分关于树的题目都是环绕遍历进行变体来解决的. 一般来说面试中遇到树的题目是用递归来解决的, 只是假设直接考察遍历. 那么一般递归的解法就过于简单了. 面试官 ...
- leetcode: 树
1. sum-root-to-leaf-numbers Given a binary tree containing digits from0-9only, each root-to-leaf pat ...
- L2-006 树的遍历 (层序遍历)
根据访问根节点与左右子树的先后顺序,二叉树一般有三种遍历方式:先序遍历.中序遍历和后序遍历. 只要给定中序遍历序列与先序或后序中的一种,可以还原二叉树结构.学习数据结构课程时,一直都只会手动构建还原二 ...
- LeetCode树专题
LeetCode树专题 98. 验证二叉搜索树 二叉搜索树,每个结点的值都有一个范围 /** * Definition for a binary tree node. * struct TreeNod ...
- 数据结构--树(遍历,红黑,B树)
平时接触树还比较少,写一篇博文来积累一下树的相关知识. 很早之前在数据结构里面学的树的遍历. 前序遍历:根节点->左子树->右子树 中序遍历:左子树->根节点->右子树 后序遍 ...
- YTU 3023: 树的遍历
原文链接:https://www.dreamwings.cn/ytu3023/2617.html 3023: 树的遍历 时间限制: 1 Sec 内存限制: 128 MB 提交: 3 解决: 2 题 ...
- 团体程序设计天梯赛-练习集L2-006. 树的遍历
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
- leetcode404-----简单的树的遍历
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- pat L2-006. 树的遍历
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
- L2-006. 树的遍历
题目链接:L2-006. 树的遍历 今天一神给我手敲二叉树模板,瞬间就领悟了,感觉自己萌萌哒! 看上去很直观! #include <iostream> #include <cstdi ...
随机推荐
- Native进程之Trace原理(转)——可直接输出某进程的栈帧——debuggerd
一. 概述 当发生ANR(Application Not Response,对于Java进程可通过kill -3向目标进程发送信号SIGNAL_QUIT, 输出相应的traces信息保存到目录/dat ...
- Mac 使用smb协议连接FTPserver
在Mac中,能够通过smb协议作为client连接到server,比如一个FTPserver,然后获取上面的共享文件. 方法: 1.在Finder菜单中点击前往 -- 连接server. 也能够Com ...
- LoadRunner 事务响应时间的组成
事务时间 一个事务的时间是指持续时间,事务会完全记录下从事务开始到事务结束之间的时间差,那么事务的时间能真实地反映业务操作的时间吗?不能,就好像人用手按秒表来记录短跑时间一样,得出的时间并不是完全准确 ...
- iOS学习之动画效果的实现
// // ViewController.m // UI-动画练习 // // Created by jzq_mac on 15/7/22. // Copyright (c) 2015年 jz ...
- 10 逻辑完善以及bug修复
进行到这里,我们应用开发已经接近尾声,我这里基本就是应用开发的记录过程,讲解的东西很少,有问题可以在评论区讨论呦.下面进入最后调整的阶段. 预览我们的应用,会发现首页的职位列表,也会显示收藏的星星图标 ...
- Fix "Unable to lock the administration directory (/var/lib/dpkg/)" in Ubuntu
While using the apt-get command or the relatively new APT package management tool in Ubuntu Linux or ...
- string operation in powershell
https://blogs.technet.microsoft.com/heyscriptingguy/2014/07/15/keep-your-hands-clean-use-powershell- ...
- mac系统下设置eclipse的补全快捷键方法
eclispe Word Completion 的默认快捷键是Alt+/eclipse Content Assist 的默认快捷键是Ctrl+Space在使用中发现Word Completion经常导 ...
- 异常备忘:java.lang.UnsupportedClassVersionError: Bad version number in .class file
转自:https://blog.csdn.net/myyugioh/article/details/7724915 今天在导入一个工程时,编译并打包到Tomcat后,发现出现java.lang.Uns ...
- 使用Asp.net Identity 创建用户 、登录代码
1.Identity 1中的注册.登录.注销代码 vs 2013中自带的注册用户代码: 1.首先创建一个ApplicationUser 初始化用户名. 2.使用UserManager创建一个用户,用使 ...