Problem Description
Easy level
Given a binary tree, return the inorder traversal of its nodes’ values. Binary Tree Inorder Traversal is to parse left child node, then root, then right child node
Example
Given binary tree {1,#,2,3},
1 2 3 4 5 6 7 |
1 \ 2 / 3 |
return [1,3,2].
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/** * @param root: The root of binary tree. * @return: inorder in ArrayList which contains node values. */ public ArrayList<Integer> inorderTraversal(TreeNode root) { ArrayList<Integer> lst = new ArrayList<Integer>(); traver(lst, root); return lst; } void traver(ArrayList<Integer> lst, TreeNode node) { if(node == null) return; if(node.left != null) traver(lst, node.left); lst.add(node.val); if(node.right != null) traver(lst, node.right); } |