p94

LeetCode 94 Binary Tree Inorder Traversal 题解

1.题目:

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

题意:

二叉树的中序遍历

2.解题思路:

http://baike.baidu.com/link?url=dcnE7bkqyBDjulzogww9CMT7UaA2H4pcDDolCbZbz0UM0B0FzAfItyGZi_WxQbPEl5En14Vz_AuJFdUpFSaKR_
了解什么是中序遍历

3.代码


[title] [] [url] [link text]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 

public class Solution {
public static void dp(TreeNode root, List<Integer> list) {

if(root == null){
return;
}else{
dp(root.left,list);//一直传递左节点当一个左节点再也没有左节点时返回
list.add(root.val);
dp(root.right,list);
}
return;
}

public static List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
if (root == null)
return list;
dp(root, list);

// for (int i : list) {
// System.out.println(i);
// }
return list;
}
}

4.一些总结: