<commit_before>---
layout: post
title:  "Binary Tree 1 - Traversal"
date:   2016-03-24 13:30:00
tags: [algorithm, leetcode, binary tree, traversal]
categories: Algorithm
---

### 1. Binary Tree Preorder Traversal
Recurse(Traverse) - Template：
{% highlight C++ %}
vector<int> preorderTraversal(TreeNode *root) {
  vector<int> res;
  preorderTraversalCore(root, res);
  return res;<commit_msg>add some tips of DFS<commit_after>---
layout: post
title:  "Binary Tree 1 - Traversal"
date:   2016-03-24 13:30:00
tags: [algorithm, leetcode, binary tree, traversal]
categories: Algorithm
---

> 二叉树问题: 考虑整棵树在该问题上的结果 和左右儿子在该问题上结果的联系

> DFS 总结：

![DFS-1](http://7xno5y.com1.z0.glb.clouddn.com/DFS-1.jpg)

<table border="2" frame="box" cellspacing="0px" style="border-collapse:collapse" valign="center">
    <tr bgcolor="lightgreen">
        <th align="center">   Traverse   </th>
        <th align="center">Divide Conquer</th>
    </tr>
    <tr>
        <td align="center">Recursion Algorithm</td>
        <td align="center">Also Recursion</td>
    </tr>
    <tr>
        <td align="center">Result in parameter</td>
        <td align="center">Result in return value</td>
    </tr>
    <tr>
        <td align="center">Top down</td>
        <td align="center">Bottom up</td>
    </tr>
</table>

### 1. Binary Tree Preorder Traversal
Recurse(Traverse) - Template：
{% highlight C++ %}
vector<int> preorderTraversal(TreeNode *root) {
  vector<int> res;
  preorderTraversalCore(root, res);
  return res;
{'fraction_non_alphanumeric': 0.16005379959650304, 'fraction_numerical': 0.026899798251513115, 'mean_word_length': 3.738853503184713, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5821059', 'n_tokens_mistral': 592, 'n_tokens_neox': 558, 'n_words': 127}