leetcode------Binary Tree Inorder Traversal

标题: Binary Tree Inorder Traversal
通过率: 36.1%
难度: 中等

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?

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

二叉树的中序遍历,直接看代码:

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public List<Integer> inorderTraversal(TreeNode root) {
12         ArrayList<Integer> result=new ArrayList<Integer>();
13         vistTree(result,root);
14         return result;
15     }
16     public void vistTree(List<Integer> temp,TreeNode root){
17         if(root!=null){
18             vistTree(temp,root.left);
19             temp.add(root.val);
20             vistTree(temp,root.right);
21         }
22     }
23 }

 

posted @ 2015-03-03 16:52  pku_smile  阅读(108)  评论(0编辑  收藏  举报