二叉树先中后序遍历(递归非递归)
Posted 雾晴
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树先中后序遍历(递归非递归)相关的知识,希望对你有一定的参考价值。
先序遍历
先序遍历也叫先根遍历,采用先访问根节点,再访问左节点,最后访问右节点
递归实现
/**
* Definition for a binary tree node.
* public class TreeNode
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode()
* TreeNode(int val) this.val = val;
* TreeNode(int val, TreeNode left, TreeNode right)
* this.val = val;
* this.left = left;
* this.right = right;
*
*
*/
class Solution
public List<Integer> preorderTraversal(TreeNode root)
List<Integer>list=new LinkedList<Integer>();
bl(root,list);
return list;
public void bl(TreeNode root,List<Integer> list)
if(root==null)
return;
list.add(root.val);
bl(root.left,list);
bl(root.right,list);
非递归实现
非递归实现借助了栈
/**
* Definition for a binary tree node.
* public class TreeNode
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode()
* TreeNode(int val) this.val = val;
* TreeNode(int val, TreeNode left, TreeNode right)
* this.val = val;
* this.left = left;
* this.right = right;
*
*
*/
class Solution
public List<Integer> preorderTraversal(TreeNode root)
return xhbl(root);
public List<Integer> xhbl(TreeNode root)
List<Integer> list=new ArrayList<>();
Stack<TreeNode> stack=new Stack<>();
while(root!=null || !stack.isEmpty())
while (root!=null)
list.add(root.val);
stack.push(root.right);
root=root.left;
root=stack.pop();
//root=root.right;
return list;
中序遍历
中序遍历的顺序为 左节点,根节点、右节点
递归实现
/**
* Definition for a binary tree node.
* public class TreeNode
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode()
* TreeNode(int val) this.val = val;
* TreeNode(int val, TreeNode left, TreeNode right)
* this.val = val;
* this.left = left;
* this.right = right;
*
*
*/
class Solution
public List<Integer> inorderTraversal(TreeNode root)
List<Integer> list=new LinkedList<Integer>();
accTre(root,list);
return list;
public void accTre(TreeNode res,List<Integer> list)
if(res==null)
return ;
accTre(res.left,list);
list.add(res.val);
accTre(res.right,list);
非递归实现
当然非递归实现也是采用了栈
/**
* Definition for a binary tree node.
* public class TreeNode
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode()
* TreeNode(int val) this.val = val;
* TreeNode(int val, TreeNode left, TreeNode right)
* this.val = val;
* this.left = left;
* this.right = right;
*
*
*/
class Solution
public List<Integer> inorderTraversal(TreeNode root)
return xhbl(root);
public List<Integer> xhbl(TreeNode root)
List<Integer> list=new ArrayList<>();
Stack<TreeNode> stack=new Stack<>();
while(root!=null || !stack.isEmpty())
while (root!=null)
stack.push(root);
root=root.left;
root=stack.pop();
list.add(root.val);
root=root.right;
return list;
后序遍历
后续遍历的顺序是左节点、右节点、根节点
递归实现
/**
* Definition for a binary tree node.
* public class TreeNode
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode()
* TreeNode(int val) this.val = val;
* TreeNode(int val, TreeNode left, TreeNode right)
* this.val = val;
* this.left = left;
* this.right = right;
*
*
*/
class Solution
public List<Integer> postorderTraversal(TreeNode root)
List<Integer> list=new LinkedList<Integer>();
hxbl(root,list);
return list;
public void hxbl(TreeNode root,List<Integer> list)
if(root==null)
return;
hxbl(root.left,list);
hxbl(root.right,list);
list.add(root.val);
非递归实现
非递归实现采用栈和临时变量,
用临时变量标识当前节点的右子节点是否被访问过,从而确定是否将当前节点输出
/**
* Definition for a binary tree node.
* public class TreeNode
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode()
* TreeNode(int val) this.val = val;
* TreeNode(int val, TreeNode left, TreeNode right)
* this.val = val;
* this.left = left;
* this.right = right;
*
*
*/
class Solution
public List<Integer> postorderTraversal(TreeNode root)
return xhbl(root);
public List<Integer> xhbl(TreeNode root)
List<Integer> list=new ArrayList<>();
Stack<TreeNode> stack=new Stack<>();
TreeNode press = null;
while(root!=null || !stack.isEmpty())
while (root!=null)
stack.push(root);
root=root.left;
root=stack.pop();
if(root.right==null || root.right==press)
list.add(root.val);
press=root;
root=null;
else
stack.push(root);
root=root.right;
return list;
以上是关于二叉树先中后序遍历(递归非递归)的主要内容,如果未能解决你的问题,请参考以下文章