二叉树指树中节点的度不大于2的有序树
二叉树相关属性解释:
深度优先DFS:前序(根左右)、中序(左根右)、后序(左右根)
广度优先BFS:层序遍历
递归方式
class Solution {
// 递归法
private List<Integer> list = new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
if(root==null) return list;
dfs(root);
return list;
}
public void dfs(TreeNode root) {
if(root==null) return;
list.add(root.val); //***
dfs(root.left);
dfs(root.right);
}
}
迭代方式(使用栈)
class Solution {
// 迭代法 不为空就push同时加入list,左右为空则pop不加入list
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> st = new Stack();
while(st.size() != 0 || root != null){
if(root != null){
st.push(root);
list.add(root.val); //入栈取值
root = root.left;
}else{
TreeNode tmp = st.pop();
root = tmp.right;
}
}
return list;
}
}
递归方式
class Solution {
// 递归方法比较简单 时间n,空间h(树的高度)
private List<Integer> list = new ArrayList<>();
if(root==null) return list;
dfs(root);
return list;
}
public void dfs(TreeNode root) {
if(root==null) return;
dfs(root.left);
list.add(root.val); //***
dfs(root.right);
}
}
迭代方式(使用栈)
class Solution {
// 迭代方法,用栈来实现
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
// 栈中存的是TreeNode
Stack<TreeNode> st = new Stack<>();
while(st.size()>0 || root != null){
if(root != null){
st.push(root);
root = root.left;
}else{
TreeNode tmp = st.pop();
list.add(tmp.val); //出栈取值
root = tmp.right;
}
}
return list;
}
}
递归方式
class Solution {
// 递归法
List<Integer> list = new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
if(root==null) return list;
dfs(root);
return list;
}
public void dfs(TreeNode root){
if(root==null) return;
dfs(root.left);
dfs(root.right);
list.add(root.val); //***
}
}
迭代方式(使用栈)
class Solution {
// 迭代法 比较特殊,中右左取值,然后逆序,仿照前序中左右写
public List<Integer> postorderTraversal(TreeNode root) {
Stack<TreeNode> st = new Stack<>();
List<Integer> list = new ArrayList<>();
while(st.size() != 0 || root != null){
if(root != null){
list.add(root.val);
st.push(root);
root = root.right;
}else{
TreeNode tmp = st.pop();
root = tmp.left;
}
}
Collections.reverse(list); //使用Collections集合工具类 逆序
return list;
}
}
迭代法(使用队列)
class Solution {
// 广度优先 迭代法 用队列Queue (深度优先的迭代法用栈Stack)
public List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
List<List<Integer>> list = new ArrayList<>();
if(root==null) return list;
q.add(root);
while(!q.isEmpty()){
// 记录队列长度
int size = q.size();
List<Integer> tmp = new ArrayList<>();
// 根据队列长度取元素,取出一个元素后,放入其左右节点
for(int i=0; i<size; i++){
TreeNode cur = q.poll();
tmp.add(cur.val);
if(cur.left != null) q.offer(cur.left);
if(cur.right != null) q.offer(cur.right);
}
list.add(tmp);
}
return list;
}
}
原文:https://www.cnblogs.com/xiangyu97/p/14472718.html