二叉树的三种遍历方式(递归 和 非递归)
2017-12-07 14:02
232 查看
//先序 递归遍历
public void preorderRecursion(TreeNode root,List list){
if(root != null){
list.add(root.val);
TreeNode left = root.left;
if(left != null){
preorderRecursion(root.left,list);
}
TreeNode right = root.right;
if(right != null){
preorderRecursion(root.right, list);
}
}
}
//先序遍历非递归
public List preorderNotRecursion(TreeNode root){
List result = new ArrayList<>();
Deque stack = new ArrayDeque<>();
TreeNode p = root;
while(!stack.isEmpty() || p != null){
if(p!=null){
stack.push(p);
result.add(p.val);
p = p.left;
}else{
TreeNode node = stack.pop();
p = node.right;
}
}
return result;
}
//中序递归
public void inorderRecursion(TreeNode root,List list){
if(root != null){
TreeNode left = root.left;
if(left != null){
inorderRecursion(root.left,list);
}
list.add(root.val);
TreeNode right = root.right;
if(right != null){
inorderRecursion(root.right, list);
}
}
}
//中序非递归遍历
public List inorderNotRecursion(TreeNode root){
List result = new ArrayList<>();
Deque stack = new ArrayDeque<>();
TreeNode p = root;
while(!stack.isEmpty() || p != null){
if(p != null){
stack.push(p);
p = p.left;
}else{
TreeNode node = stack.pop();
result.add(node.val);
p = node.right;
}
}
return result;
}
//后序递归
public void postorderRecursion(TreeNode root,List list){
if(root != null){
TreeNode left = root.left;
if(left != null){
postorderRecursion(root.left,list);
}
TreeNode right = root.right;
if(right != null){
postorderRecursion(root.right, list);
}
list.add(root.val);
}
}
//后序非递归遍历
public List postorderNotRecursion(TreeNode root){
LinkedList result = new LinkedList<>();
Deque stack = new ArrayDeque<>();
TreeNode p = root;
while(!stack.isEmpty() || p!= null){
if(p != null){
stack.push(p);
result.addFirst(p.val);
p = p.right;
}else{
TreeNode node = stack.pop();
p = node.left;
}
}
return result;
}
public void preorderRecursion(TreeNode root,List list){
if(root != null){
list.add(root.val);
TreeNode left = root.left;
if(left != null){
preorderRecursion(root.left,list);
}
TreeNode right = root.right;
if(right != null){
preorderRecursion(root.right, list);
}
}
}
//先序遍历非递归
public List preorderNotRecursion(TreeNode root){
List result = new ArrayList<>();
Deque stack = new ArrayDeque<>();
TreeNode p = root;
while(!stack.isEmpty() || p != null){
if(p!=null){
stack.push(p);
result.add(p.val);
p = p.left;
}else{
TreeNode node = stack.pop();
p = node.right;
}
}
return result;
}
//中序递归
public void inorderRecursion(TreeNode root,List list){
if(root != null){
TreeNode left = root.left;
if(left != null){
inorderRecursion(root.left,list);
}
list.add(root.val);
TreeNode right = root.right;
if(right != null){
inorderRecursion(root.right, list);
}
}
}
//中序非递归遍历
public List inorderNotRecursion(TreeNode root){
List result = new ArrayList<>();
Deque stack = new ArrayDeque<>();
TreeNode p = root;
while(!stack.isEmpty() || p != null){
if(p != null){
stack.push(p);
p = p.left;
}else{
TreeNode node = stack.pop();
result.add(node.val);
p = node.right;
}
}
return result;
}
//后序递归
public void postorderRecursion(TreeNode root,List list){
if(root != null){
TreeNode left = root.left;
if(left != null){
postorderRecursion(root.left,list);
}
TreeNode right = root.right;
if(right != null){
postorderRecursion(root.right, list);
}
list.add(root.val);
}
}
//后序非递归遍历
public List postorderNotRecursion(TreeNode root){
LinkedList result = new LinkedList<>();
Deque stack = new ArrayDeque<>();
TreeNode p = root;
while(!stack.isEmpty() || p!= null){
if(p != null){
stack.push(p);
result.addFirst(p.val);
p = p.right;
}else{
TreeNode node = stack.pop();
p = node.left;
}
}
return result;
}
相关文章推荐
- 二叉树三种遍历方式 递归与非递归实现 总结
- 详细讲解二叉树三种遍历方式的递归与非递归实现
- java使用递归,非递归方式实现二叉树的三种常见遍历方式
- 二叉树三种非递归的遍历方式
- 二叉树的三种遍历方式:递归、栈、循环 分类: C/C++ 数据结构与算法 2015-06-21 09:47 269人阅读 评论(0) 收藏
- 二叉树的三种遍历方式的递归实现和迭代实现
- 二叉树的三种遍历方式的循环和递归的实现方式
- 二叉树的三种非递归遍历方式。
- 详细讲解二叉树三种遍历方式的递归与非递归实现
- 二叉树的三种遍历方式:递归、栈、循环
- 二叉树的三种遍历方式(递归和非递归的实现方法)
- 详细讲解二叉树三种遍历方式的递归与非递归实现
- 二叉树三种遍历方式非递归实现
- 二叉树的存储方式以及递归和非递归的三种遍历方式
- 二叉树的三种遍历方式(递归与非递归详细实现)
- 二叉树的三种遍历方式(递归、非递归和Morris遍历)
- 二叉树三种递归遍历方式
- 二叉树的存储方式以及递归和非递归的三种遍历方式
- 详细讲解二叉树三种遍历方式的递归与非递归实现
- 【python中二叉树的实现】python中二叉树的创建、三种方式递归遍历和非递归遍历