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].
思路:二叉树的中序遍历
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,3,2].
思路:二叉树的中序遍历
package leetcode;
import java.util.ArrayList;
import java.util.List;
public class BinaryTreeInorderTraversal {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
if(root == null) return list;
if(root.left != null) list.addAll(inorderTraversal(root.left));
list.add(root.val);
if(root.right != null) list.addAll(inorderTraversal(root.right));
return list;
}
}