[leetcode-二叉树中序遍历]--94. Binary Tree Inorder Traversal

来源:互联网 发布:node读文件 promise 编辑:程序博客网 时间:2024/05/17 08:54

Question 94. Binary Tree Inorder Traversal

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].

二叉树的中序遍历

和先序遍历基本一样,只是改成中序遍历:

/** * @param root * @return */public static List<Integer> inorderTraversal(TreeNode root) {    List<Integer> res= new ArrayList<Integer>();    inHelper(root, res);    return res;}private static void inHelper(TreeNode root, List<Integer> in) {    if(root==null) return;    inHelper(root.left, in);    in.add(root.val);    inHelper(root.right,in);}

完整的github地址:
https://github.com/leetcode-hust/leetcode/blob/master/louyuting/src/leetcode/Question94.java

0 0