Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [1,3,2]
.
Note: Recursive solution is trivial, could you do it iteratively?
中根遍历(左子树 根节点 右子数)
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private ArrayList<Integer> list;
public ArrayList<Integer> inorderTraversal(TreeNode root) {
list = new ArrayList<Integer>();
sort(root);
return list;
}
public void sort(TreeNode root){
if(root!=null){
if(root.left!=null)
sort(root.left);
list.add(root.val);
sort(root.right);
}
}
}