给定一个二叉树的根节点 root ,返回它的 中序 遍历。
/**
-
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 inorderTraversal(TreeNode root) { List res = new ArrayList(); inorder(root,res); return res; }
public void inorder(TreeNode root,List res){ if( root == null){ return; } inorder(root.left,res); res.add(root.val); inorder(root.right,res); } }