File tree Expand file tree Collapse file tree 13 files changed +36
-9
lines changed
03-Non-Recursive-Implementation-of-a-Recursive-Algorithm
04-Binary-Tree-Level-Order-Traversal
02-Recursion-and-Stack/src
03-Non-Recursive-Implementation-of-a-Recursive-Algorithm/src Expand file tree Collapse file tree 13 files changed +36
-9
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,8 @@ using namespace std;
6
6
// / 144. Binary Tree Preorder Traversal
7
7
// / https://leetcode.com/problems/binary-tree-preorder-traversal/description/
8
8
// / 二叉树的前序遍历
9
+ // / 时间复杂度: O(n), n为树的节点个数
10
+ // / 空间复杂度: O(h), h为树的高度
9
11
10
12
// / Definition for a binary tree node.
11
13
struct TreeNode {
Original file line number Diff line number Diff line change @@ -6,6 +6,8 @@ using namespace std;
6
6
// / 94. Binary Tree Inorder Traversal
7
7
// / https://leetcode.com/problems/binary-tree-inorder-traversal/solution/
8
8
// / 二叉树的中序遍历
9
+ // / 时间复杂度: O(n), n为树的节点个数
10
+ // / 空间复杂度: O(h), h为树的高度
9
11
10
12
// / Definition for a binary tree node.
11
13
struct TreeNode {
Original file line number Diff line number Diff line change @@ -6,6 +6,8 @@ using namespace std;
6
6
// / 145. Binary Tree Postorder Traversal
7
7
// / https://leetcode.com/problems/binary-tree-postorder-traversal/description/
8
8
// / 二叉树的后序遍历
9
+ // / 时间复杂度: O(n), n为树的节点个数
10
+ // / 空间复杂度: O(h), h为树的高度
9
11
10
12
// / Definition for a binary tree node.
11
13
struct TreeNode {
Original file line number Diff line number Diff line change @@ -8,6 +8,8 @@ using namespace std;
8
8
// / 144. Binary Tree Preorder Traversal
9
9
// / https://leetcode.com/problems/binary-tree-preorder-traversal/description/
10
10
// / 非递归的二叉树的前序遍历
11
+ // / 时间复杂度: O(n), n为树的节点个数
12
+ // / 空间复杂度: O(h), h为树的高度
11
13
12
14
// / Definition for a binary tree node.
13
15
struct TreeNode {
Original file line number Diff line number Diff line change @@ -6,6 +6,8 @@ using namespace std;
6
6
// / 94. Binary Tree Inorder Traversal
7
7
// / https://leetcode.com/problems/binary-tree-inorder-traversal/solution/
8
8
// / 非递归的二叉树的中序遍历
9
+ // / 时间复杂度: O(n), n为树的节点个数
10
+ // / 空间复杂度: O(h), h为树的高度
9
11
10
12
// / Definition for a binary tree node.
11
13
struct TreeNode {
Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ using namespace std;
7
7
// / 145. Binary Tree Postorder Traversal
8
8
// / https://leetcode.com/problems/binary-tree-postorder-traversal/description/
9
9
// / 非递归的二叉树的后序遍历
10
+ // / 时间复杂度: O(n), n为树的节点个数
11
+ // / 空间复杂度: O(h), h为树的高度
10
12
11
13
// / Definition for a binary tree node.
12
14
struct TreeNode {
Original file line number Diff line number Diff line change 5
5
6
6
using namespace std ;
7
7
8
+ // / 102. Binary Tree Level Order Traversal
9
+ // / https://leetcode.com/problems/binary-tree-level-order-traversal/description/
10
+
8
11
// / Definition for a binary tree node.
9
12
struct TreeNode {
10
13
int val;
@@ -22,23 +25,23 @@ class Solution {
22
25
return res;
23
26
24
27
queue<pair<TreeNode*,int >> q;
25
- q.push ( make_pair ( root , 0 ) );
28
+ q.push (make_pair (root, 0 ));
26
29
27
- while ( !q.empty () ){
30
+ while (!q.empty ()){
28
31
29
32
TreeNode* node = q.front ().first ;
30
33
int level = q.front ().second ;
31
34
q.pop ();
32
35
33
- if ( level == res.size () )
34
- res.push_back ( vector<int >() );
36
+ if (level == res.size ())
37
+ res.push_back (vector<int >());
35
38
assert ( level < res.size () );
36
39
37
- res[level].push_back ( node->val );
38
- if ( node->left )
39
- q.push ( make_pair (node->left , level + 1 ) );
40
- if ( node->right )
41
- q.push ( make_pair (node->right , level + 1 ) );
40
+ res[level].push_back (node->val );
41
+ if (node->left )
42
+ q.push (make_pair (node->left , level + 1 ));
43
+ if (node->right )
44
+ q.push (make_pair (node->right , level + 1 ));
42
45
}
43
46
44
47
return res;
Original file line number Diff line number Diff line change 4
4
/// 94. Binary Tree Inorder Traversal
5
5
/// https://leetcode.com/problems/binary-tree-inorder-traversal/solution/
6
6
/// 二叉树的中序遍历
7
+ /// 时间复杂度: O(n), n为树的节点个数
8
+ /// 空间复杂度: O(h), h为树的高度
7
9
public class Solution094 {
8
10
9
11
// Definition for a binary tree node.
Original file line number Diff line number Diff line change 4
4
/// 144. Binary Tree Preorder Traversal
5
5
/// https://leetcode.com/problems/binary-tree-preorder-traversal/description/
6
6
/// 二叉树的前序遍历
7
+ /// 时间复杂度: O(n), n为树的节点个数
8
+ /// 空间复杂度: O(h), h为树的高度
7
9
public class Solution144 {
8
10
9
11
// Definition for a binary tree node.
Original file line number Diff line number Diff line change 4
4
/// 145. Binary Tree Postorder Traversal
5
5
/// https://leetcode.com/problems/binary-tree-postorder-traversal/description/
6
6
/// 二叉树的后序遍历
7
+ /// 时间复杂度: O(n), n为树的节点个数
8
+ /// 空间复杂度: O(h), h为树的高度
7
9
public class Solution145 {
8
10
9
11
// Definition for a binary tree node.
Original file line number Diff line number Diff line change 5
5
/// 94. Binary Tree Inorder Traversal
6
6
/// https://leetcode.com/problems/binary-tree-inorder-traversal/solution/
7
7
/// 非递归二叉树的中序遍历
8
+ /// 时间复杂度: O(n), n为树的节点个数
9
+ /// 空间复杂度: O(h), h为树的高度
8
10
public class Solution094 {
9
11
10
12
// Definition for a binary tree node.
Original file line number Diff line number Diff line change 5
5
/// 144. Binary Tree Preorder Traversal
6
6
/// https://leetcode.com/problems/binary-tree-preorder-traversal/description/
7
7
/// 非递归二叉树的前序遍历
8
+ /// 时间复杂度: O(n), n为树的节点个数
9
+ /// 空间复杂度: O(h), h为树的高度
8
10
public class Solution144 {
9
11
10
12
// Definition for a binary tree node.
Original file line number Diff line number Diff line change 5
5
/// 145. Binary Tree Postorder Traversal
6
6
/// https://leetcode.com/problems/binary-tree-postorder-traversal/description/
7
7
/// 非递归的二叉树的后序遍历
8
+ /// 时间复杂度: O(n), n为树的节点个数
9
+ /// 空间复杂度: O(h), h为树的高度
8
10
public class Solution145 {
9
11
10
12
// Definition for a binary tree node.
You can’t perform that action at this time.
0 commit comments