https://leetcode.com/problems/binary-tree-inorder-traversal/description/
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3]
,
1
2
/
3
return [1,3,2]
.
time: o(n) space:o(n)
1 public List<Integer> inorderTraversal(TreeNode root) {
2 // Write your solution here
3 if(root == null ) return new ArrayList<Integer>() ;
4 List<Integer> res = new ArrayList<Integer>() ;
5 traverse(root, res) ;
6 return res ;
7 }
8
9 private void traverse(TreeNode root, List<Integer> res){
10 //base case
11 if(root == null ) return ;
12 traverse(root.left, res);
13 res.add(root.val);
14 traverse(root.right, res) ;
15 }