题目
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,3,2]
递归实现二叉树的前、中、后序遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
inorder(root,result);
return result;
}
public void inorder(TreeNode cur,List<Integer> res){
if(cur ==null) return;
inorder(cur.left,res);
res.add(cur.val);
inorder(cur.right,res);
}
public void preorder(TreeNode cur,List<Integer> res){
if(cur ==null) return;
res.add(cur.val);
inorder(cur.left,res);
inorder(cur.right,res);
}
public void afterorder(TreeNode cur,List<Integer> res){
if(cur ==null) return;
inorder(cur.left,res);
inorder(cur.right,res);
res.add(cur.val);
}
}