前序就是根左右,中序就是左根右,后序就是左右根
树结构
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
中序
void in(List<Integer> list,TreeNode root){
if(root == null)return;
in(list,root.left);
list.add(root.val);
in(list,root.right);
}
前序
void pre(List<Integer> list,TreeNode root){
if(root == null)return;
list.add(root.val);
pre(list,root.left);
pre(list,root.right);
}
后序
void post(List<Integer> list,TreeNode root){
if(root == null)return;
post(list,root.left);
post(list,root.right);
list.add(root.val);
}