Binary Tree Traversal 二叉树的前中后序遍历
[抄题]:二叉树前序遍历
[思维问题]:
不会递归。三要素:下定义、拆分问题(eg root-root.left)、终止条件
[一句话思路]:
节点非空时往左移,否则新取一个点 再往右移。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
不要提起加入root节点。
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[总结]:
[复杂度]:Time complexity: O(n) Space complexity: O(n) (lgn for B-BST)
[英文数据结构,为什么不用别的数据结构]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/*
* @param root: A Tree
* @return: Preorder in ArrayList which contains node values.
*/
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode p = root; if (root == null) {
return result;
} while (!stack.isEmpty() || p != null) {
if (p != null) {
stack.push(p);
result.add(p.val);
p = p.left;
}
else {
p = stack.pop();
p = p.right;
}
}
return result;
}
}
[抄题]:二叉树中序遍历
[思维问题]:
不会递归。三要素:下定义、拆分问题(eg root-root.left)、终止条件
[一句话思路]:
左边走完了再放数
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[总结]:
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构,为什么不用别的数据结构]:
stack先进先出
[其他解法]:

遍历法:先写主函数,再写遍历函数
分治法:分开的时候要调用函数:left = 函数(root.left)
[Follow Up]:
[LC给出的题目变变变]:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/*
* @param root: A Tree
* @return: Preorder in ArrayList which contains node values.
*/
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode p = root; if (root == null) {
return result;
} while (!stack.isEmpty() || p != null) {
if (p != null) {
stack.push(p);
p = p.left;
}
else {
p = stack.pop();
result.add(p.val);
p = p.right;
}
}
return result;
}
}
[抄题]:二叉树后序遍历
[思维问题]:
不会递归。三要素:下定义、拆分问题(eg root-root.left)、终止条件
[一句话思路]:
前序遍历全都反过来:节点添加顺序、提前添加用
result.addFirst
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[总结]:
[复杂度]:Time complexity: O() Space complexity: O()
[英文数据结构,为什么不用别的数据结构]:
[其他解法]:
[Follow Up]:

[LC给出的题目变变变]:
public List<Integer> postorderTraversal(TreeNode root) {
LinkedList<Integer> result = new LinkedList<>();
Deque<TreeNode> stack = new ArrayDeque<>();
TreeNode p = root;
while(!stack.isEmpty() || p != null) {
if(p != null) {
stack.push(p);
result.addFirst(p.val); // Reverse the process of preorder
p = p.right; // Reverse the process of preorder
} else {
TreeNode node = stack.pop();
p = node.left; // Reverse the process of preorder
}
}
return result;
}
Binary Tree Traversal 二叉树的前中后序遍历的更多相关文章
- POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)
链接:poj.org/problem?id=2255 本文链接:http://www.cnblogs.com/Ash-ly/p/5463375.html 题意: 分别给你一个二叉树的前序遍历序列和中序 ...
- [C++] 非递归实现前中后序遍历二叉树
目录 前置技能 需求描述 binarytree.h 具体实现 binarytree.cpp main.cpp 网上代码一搜一大片,大同小异咯. 书上的函数实现代码甚至更胜一筹,而且抄一遍就能用,唯一问 ...
- Qt实现 动态化遍历二叉树(前中后层次遍历)
binarytree.h 头文件 #ifndef LINKEDBINARYTREE_H #define LINKEDBINARYTREE_H #include<c++/algorithm> ...
- C++二叉树前中后序遍历(递归&非递归)统一代码格式
统一下二叉树的代码格式,递归和非递归都统一格式,方便记忆管理. 三种递归格式: 前序遍历: void PreOrder(TreeNode* root, vector<int>&pa ...
- 数据结构-C语言递归实现树的前中后序遍历
#include <stdio.h> #include <stdlib.h> typedef struct tree { int number ; struct tree *l ...
- C++实现对树的创建和前中后序遍历
#include<iostream>#include<stdio.h> using namespace std; class BitNode{ public: char dat ...
- 前中后序递归遍历树的体会 with Python
前序:跟->左->右 中序:左->根->右 后序:左>右->根 采用递归遍历时,编译器/解释器负责将递归函数调用过程压入栈并保护现场,在不同位置处理根节点即可实现不 ...
- java实现二叉树的前中后遍历(递归和非递归)
这里使用下图的二叉树作为例子: 首先建立树这个类: public class Node { private int data; private Node leftNode; private Node ...
- 二叉树前中后/层次遍历的递归与非递归形式(c++)
/* 二叉树前中后/层次遍历的递归与非递归形式 */ //*************** void preOrder1(BinaryTreeNode* pRoot) { if(pRoot==NULL) ...
随机推荐
- CSS源码之纯css3制作的哆啦a梦图片
本文章向大家介绍一个纯css3制作的哆啦a梦图像,主要巧妙的使用了css3的border-radius属性,需要的朋友介意参考一下本文章的源码. 效果图: 源码 <!doctype html&g ...
- php变量详细讲解
变量是用于存储信息的"容器". 定义一个变量的语法: $变量名 = 值; 使用变量的例子: <?php $x=5; $y=6; $z=$x+$y; echo $z; ?> ...
- jenkins将构建成功或失败的信息发送给指定URL(eg: pomelo采用jenkins持续集成)
先提供一个思路供大家参考,想将构建成功或者失败的信息发送给指定URL的话,可以这样:1.A构建后触发另一个构建B,构建B执行某个插件2.插件的功能: (1)利用jenkins API获取构建A最 ...
- pure框架
内容: 1.介绍与入门 2.基础使用 参考资料: pure中文文档:https://www.purecss.cn/ pure实例:https://www.purecss.cn/layouts.html ...
- 手贱!使用django,在数据库直接删除了表
莫名其妙的错误. 删除了migreation文件,并且更换了数据库. 1.直接makemigrations + migrate error: no change ?? WTF 2.makemi ...
- UVA375
题意: 已知等腰三角形的高H,底边长B,这时有一个内切圆C, 以内切圆C和长度为B对应的角的角平分线的交点做切线. 切线与角平分线相交,此时切线,和俩边又会出现一个小的等腰三角形,也有一个小的内切圆C ...
- leetcode134
class Solution { public: inline int get_next(int idx, int size) { ? : idx+; } int aux(int idx, vecto ...
- 4.Spring中使用Log4j
转自:https://blog.csdn.net/luohai859/article/details/52250807 这里要实现web项目中利用Spring来使用Log4j (1)接上面的工程,然后 ...
- J2SE 8的注解
1. 注解概念 (1) 注解格式 modifiers @interface AnnotationName { type elementName(); type elementName() defaul ...
- MVC中数据传递 ViewBag的使用
ViewBag MVC3中 ViewBag.ViewData和TempData的使用和差别 在MVC3開始.视图数据能够通过ViewBag属性訪问.在MVC2中则是使用ViewData.MVC3中保留 ...