Other Traversal Problem

Other Traversal Problem

题目描述1:

给定一棵搜索二叉树的头结点,要求找到树中出现次数最多的那个数。

例子:

LeetCode501

解题思路:

最基础的方法是遍历这棵树的所有节点,然后将节点保存到哈希表中,而后从哈希表中找到出现最多的那个数。自然有其他的解法,待补全。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
vector<int> findMode(TreeNode* root) {
map<int, int>hash;
helper(root, hash);
vector<int> res;
int most = 1;
for (auto it = hash.begin(); it != hash.end(); it++){
if (it->second > most) most = it->second;
}
for (auto it = hash.begin(); it != hash.end(); it++){
if (it->second == most) res.push_back(it->first);
}
return res;
}
void helper(TreeNode* root, map<int,int>& hash){
if (!root) return;
helper(root->left, hash);
hash[root->val]++;
helper(root->right, hash);
}
};

题目描述2:

给定一棵树的头结点,然后要求找到所有的子树和中最经常出现的那个数。

例子:

LeetCode508

解题思路:

主要的思路和上一题类似,用vector保存子树中的节点和,然后遍历这个和,找到出现最多次的那个数。

代码如下:

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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> findFrequentTreeSum(TreeNode* root) {
vector<int> vec;
findSumOfNode(root, vec);
map<int, int> hash;
vector<int> res;
for (int i = 0; i < vec.size(); i++){
hash[vec[i]]++;
}
int most = 1;
for (auto it = hash.begin(); it != hash.end(); it++){
if (it->second > most) most = it->second;
}
for (auto it = hash.begin(); it != hash.end(); it++){
if (it->second == most) res.push_back(it->first);
}
return res;
}
int findSumOfNode(TreeNode* node, vector<int>& vec){
if (!node) return 0;
if (!node->left && !node->right){
vec.push_back(node->val);
return node->val;
}
int left = findSumOfNode(node->left, vec);
int right = findSumOfNode(node->right, vec);
int res = left + right + node->val;
vec.push_back(res);
return res;
}
};

题目描述3:

给定一棵搜索二叉树的头结点,要求找到树中的第k小的的值。

例子:

LeetCode230

解题思路:

因为是搜索二叉树,所以我们可以中序遍历整个树,然后返回第k个数即可。

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
//树中的k小数
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
vector<int> vec;
inOrder(root, vec);
return vec[k - 1];
}
void inOrder(TreeNode* head, vector<int>& vec){
if (head == NULL) return;
inOrder(head->left, vec);
vec.push_back(head->val);
inOrder(head->right, vec);
}
};

题目描述4:

给定一棵树的头结点,要求找到树的最小深度。

例子:

LeetCode111

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int minDepth(TreeNode* root) {
if (!root) return 0;
if (!root->left && !root->right) return 1;
if (!root->left) return min(root->right) + 1;
if (!root->right) return min(root->left) + 1;
return min(root);
}
int min(TreeNode* root) {
if (!root->left && !root->right) return 1;
int left = (root->left) ? (min(root->left) + 1) : INT_MAX;
int right = (root->right) ? (min(root->right) + 1): INT_MAX;
return left > right ? right : left;
}
};

题目描述4:

给定一棵树的头结点,要求找到树的最大深度。

例子:

LeetCode104

代码如下:

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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
#include<queue>
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == NULL)
return 0;
int res = 0;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
res++;
for (int i = 0, n = q.size(); i < n; i++){
TreeNode* p = q.front();
q.pop();
if (p->left != NULL)
q.push(p->left);
if (p->right != NULL)
q.push(p->right);
}
}
return res;
}
};

题目描述5:

给定一棵树的头结点,要求找到所有左叶节点的和。

例子:

LeetCode404

代码如下:

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
//左侧子树的叶节点的和
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
int res = 0;
preOrder(root, res);
return res;
}
void preOrder(TreeNode* head, int& res){
if (head == NULL)
return;
if (head->left != NULL && head->left->left == NULL && head->left->right == NULL){
res = res + head->left->val;
preOrder(head->right, res);
}else{
preOrder(head->left, res);
preOrder(head->right, res);
}
}
};
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
int res = 0;
helper(root, res);
return res;
}
void helper(TreeNode* root, int& left){
if (!root) return;
if (root->left && (!root->left->left && !root->left->right)){
left += root->left->val;
helper(root->right, left);
}else{
helper(root->left, left);
helper(root->right, left);
}
}
};

题目描述6:

LeetCode337

代码如下:

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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int rob(TreeNode* root) {
if (!root) return 0;
int rootValue = root->val;
int left = 0;
int right = 0;
if (root->left){
int l1 = rob(root->left->left);
int l2 = rob(root->left->right);
rootValue += l1 + l2;
left = rob(root->left);
}
if (root->right){
int r1 = rob(root->right->left);
int r2 = rob(root->right->right);
rootValue += r1 + r2;
right = rob(root->right);
}
if (rootValue < (left + right))
return left + right;
return rootValue;
}
};