Given the root node of a binary tree, return its middle-order traversal.
/ * *
- Definition for a binary tree node.
- public class TreeNode {
-
int val; Copy the code
-
TreeNode left; Copy the code
-
TreeNode right; Copy the code
-
TreeNode() {} Copy the code
-
TreeNode(int val) { this.val = val; } Copy the code
-
TreeNode(int val, TreeNode left, TreeNode right) { Copy the code
-
this.val = val; Copy the code
-
this.left = left; Copy the code
-
this.right = right; Copy the code
-
} Copy the code
- }
*/ class Solution { public List inorderTraversal(TreeNode root) { List res = new ArrayList(); inorder(root,res); return res; }
public void inorder(TreeNode root,List<Integer> res){
if( root == null){
return;
}
inorder(root.left,res);
res.add(root.val);
inorder(root.right,res);
}
Copy the code
}