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) ...
随机推荐
- js在table指定tr行上或底下添加tr行
js在table指定tr行上或下面添加tr行 function onAddTR(trIndex) { var tb = document.getElementB ...
- .Net2.0部署在IIS8.5上的问题
请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理. 到"应用程序池"里找网站对应的应用程序池(右击网站-> 高级设置),双击程序池, 看程序池是否也网站的net ...
- Linux火焰图-centos
centos7.5mini安装 yum install -y yum-utils perf debuginfo-install -y perf #debuginfo-install下载了305MB的文 ...
- selenium自动化测试通过localstorage绕过登陆
引言: 做自动化测试,尤其是通过page object模式做UI自动化测试,登陆是个很麻烦的事情,比如你想对某个页面进行测试,一般直接链接到那个页面是不可能的,总是需要先登陆,然后刷新页面才能到想要的 ...
- uva-10112-计算几何
题意:给你一些点,求这些点组成的三角形面积最大,而且三角形内不能包含其他点 #include <iostream> #include <math.h> #include< ...
- 网际协议版本4(IPv4)
IP是一种不可靠的无连接数据报协议-一种尽最大努力交付的服务,尽最大努力一词的意思是IP分组可能会损坏,丢失,失序或延迟到达,并且可能给网络带来拥塞. 网络层的分组称为数据报.是一个可变长度的分组.由 ...
- 开发者必看|Android 8.0 新特性及开发指南
背景介绍 谷歌2017 I/O开发者大会今年将于5月17-19日在美国加州举办.大会将跟往年一样发布最新的 Android 系统,今年为 Android 8.0.谷歌在今年3 月21日发布 Andro ...
- spring security 表单认证的流程
spring security表单认证过程 表单认证过程 Spring security的表单认证过程是由org.springframework.security.web.authentication ...
- leetcode521
public class Solution { public int FindLUSlength(string a, string b) { : Math.Max(a.Length, b.Length ...
- Spring mvc RequestContextHolder分析
转载: http://blog.csdn.net/zzy7075/article/details/53559902