-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_dfs_n.cpp
52 lines (43 loc) · 1 KB
/
AC_dfs_n.cpp
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_dfs_n.cpp
* Create Date: 2014-12-22 11:14:35
* Descripton: dfs, recursive
* thank zlx
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
// Definition for binary tree
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
private:
vector<vector<int> >ans;
public:
void DFS(TreeNode *curNode, int depth) {
if (!curNode)
return;
// push back
if (depth >= ans.size())
ans.push_back(vector<int>(0));
ans[depth].push_back(curNode->val);
// recursive
if (curNode->left)
DFS(curNode->left, depth + 1);
if (curNode->right)
DFS(curNode->right, depth + 1);
}
vector<vector<int> > levelOrder(TreeNode *root) {
ans.clear();
DFS(root, 0);
return ans;
}
};
int main() {
return 0;
}