手把手教你二叉树的四种遍历方式
Posted *平芜尽处是春山*
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手把手教你二叉树的四种遍历方式相关的知识,希望对你有一定的参考价值。
二叉树的四种遍历
前序遍历
力扣144. 二叉树的前序遍历
1、递归写法
class Solution
List<Integer> ret = new LinkedList<> ();
public List<Integer> preorderTraversal(TreeNode root)
if(root == null)
return ret;
ret.add(root.val);
preorderTraversal(root.left);
preorderTraversal(root.right);
return ret;
运行截图:
2、迭代写法
class Solution
List<Integer> ret = new LinkedList<> ();
public List<Integer> preorderTraversal(TreeNode root)
if(root == null)
return ret;
Stack<TreeNode> stack = new Stack<> ();
stack.push(root);
while(!stack.isEmpty())
TreeNode node = stack.pop();
ret.add(node.val);
if(node.right != null)
stack.push(node.right);
if(node.left != null)
stack.push(node.left);
return ret;
运行截图:
中序遍历
力扣94. 二叉树的中序遍历
1、递归写法
class Solution
public List<Integer> inorderTraversal(TreeNode root)
List<Integer> ret = new LinkedList<> ();
if(root == null)
return ret;
List<Integer> ans = inorderTraversal(root.left);
ans.add(root.val);
ans.addAll(inorderTraversal(root.right));
return ans;
运行截图:
2、迭代写法
class Solution
public List<Integer> inorderTraversal(TreeNode root)
List<Integer> ret = new LinkedList<> ();
if(root == null)
return ret;
Stack<TreeNode> stack = new Stack<> ();
TreeNode cur = root;
while(cur != null || (!stack.isEmpty()))
while(cur != null)
stack.push(cur);
cur = cur.left;
cur = stack.pop();
ret.add(cur.val);
cur = cur.right;
return ret;
运行截图:
后序遍历
1、递归写法
class Solution
private List list=new ArrayList();
public List<Integer> postorderTraversal(TreeNode root)
if(root == null) return list;
postorderTraversal(root.left);
postorderTraversal(root.right);
list.add(root.val);
return list;
运行截图:
2、迭代写法
class Solution
public List<Integer> postorderTraversal(TreeNode root)
LinkedList<Integer> list=new LinkedList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode p = root;
while (p != null)
list.push(p.val);
if (p.left!= null)
stack.push(p.left);
p = p.right;
if (p == null && stack.size() > 0)
p = stack.pop();
return list;
运行截图:
3、常规写法
class Solution
public List<Integer> postorderTraversal(TreeNode root)
List<Integer> ret = new LinkedList<> ();
if(root == null)
return ret;
Stack<TreeNode> stack = new Stack<> ();
TreeNode cur = root;
TreeNode prev = null;
while(cur != null || !(stack.isEmpty()))
while(cur != null)
stack.push(cur);
cur = cur.left;
cur = stack.pop();
if(cur.right == null || cur.right == prev)
ret.add(cur.val);
prev = cur;
cur = null;
else
stack.push(cur);
cur = cur.right;
return ret;
运行截图:
层序遍历
力扣102. 二叉树的层序遍历
class Solution
public List<List<Integer>> levelOrder(TreeNode root)
List<List<Integer>> ret = new LinkedList<> ();
if(root == null)
return ret;
Queue<TreeNode> queue = new LinkedList<> ();
queue.offer(root);
while(!queue.isEmpty())
List<Integer> level = new LinkedList<> ();
int size = queue.size();
for(int i = 0;i < size;i++)
TreeNode cur = queue.poll();
level.add(cur.val);
if(cur.left != null)
queue.offer(cur.left);
if(cur.right != null)
queue.offer(cur.right);
ret.add(level);
return ret;
运行截图:
以上是关于手把手教你二叉树的四种遍历方式的主要内容,如果未能解决你的问题,请参考以下文章
二叉树的四种遍历方法(前序遍历中序遍历后序遍历层序遍历)有图有真相!!!
二叉树的四种遍历方法(前序遍历中序遍历后序遍历层序遍历)有图有真相!!!