-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution101.hpp
More file actions
68 lines (60 loc) · 1.61 KB
/
Copy pathSolution101.hpp
File metadata and controls
68 lines (60 loc) · 1.61 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// Solution101.hpp
// Algorithm
//
// Created by Pancf on 2019/12/29.
// Copyright © 2019 Pancf. All rights reserved.
//
#ifndef Solution101_hpp
#define Solution101_hpp
#include <stdio.h>
class Solution101 {
/**
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3] is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
================================================================================================
Accept details:
Runtime: 4 ms, faster than 84.36% of C++ online submissions for Symmetric Tree.
Memory Usage: 14.7 MB, less than 84.75% of C++ online submissions for Symmetric Tree.
思路:这题用递归比迭代简单多了
*/
public:
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
bool isSymmetric(TreeNode* root);
static void test() {
TreeNode root{1};
TreeNode node1{2};
TreeNode node2{2};
TreeNode node3{3};
TreeNode node4{4};
TreeNode node5{4};
TreeNode node6{3};
root.left = &node1;
root.right = &node2;
node1.left = &node3;
node1.right = &node4;
node2.left = &node5;
node2.right = &node6;
Solution101 s = Solution101();
printf("%d\n", s.isSymmetric(&root));
}
};
#endif /* Solution101_hpp */