1 题目
2 思路
题目的大意就是求树根节点到叶子结点的路径,然后根据路径上的权值求出每条路径的值,最后累加得到所有路径的值。
求路径,使用先序遍历,每次记录遍历到的每层节点,当该节点的左右孩子均为空时,该节点为叶子节点,计算路径值。
3 代码
- 1,
depth 方法中,每次value值,都逐层累加计算路径值。 - 2,
depth2 方法2中,每次记录路径值,当遇到叶节点时,计算路径值。
class Solution {
public:
int res = 0;
void depth(TreeNode* root, int value){
if(root != nullptr){
value = (value<<1) + root->val;
if(root->left == nullptr && root->right == nullptr){
res += value;
return;
}
depth(root->left, value);
depth(root->right, value);
}
}
void depth2(TreeNode* root, vector<int> path, int deep){
if(root == nullptr) return;
path[deep] = root->val;
if(root->left == nullptr && root->right == nullptr){
for(int d = 0;d <= deep;d++){
res += path[d] * pow(2, deep - d);
}
return;
}
depth2(root->left, path, deep + 1);
depth2(root->right, path, deep + 1);
}
int sumRootToLeaf(TreeNode* root) {
depth(root, 0);
return res;
}
};
4 测试用例
#include <iostream>
#include <vector>
#include <stack>
#include <cmath>
#include <deque>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
void createBiTNode(TreeNode** treeNode, deque<int> dataDeq) {
int nullTreeNodeValue = -1;
cout<<dataDeq.size()<<endl;
for(size_t i = 1; ; i++){
int fullBinaryTreeNum = pow(2, i) - 1;
if(dataDeq.size() <= fullBinaryTreeNum){
while(dataDeq.size() < fullBinaryTreeNum){
dataDeq.push_back(nullTreeNodeValue);
}
break;
}
}
char data;
data = dataDeq.front();
dataDeq.pop_front();
TreeNode * node = new TreeNode();
*treeNode = node;
(*treeNode)->val = data;
deque<TreeNode**> nodeDeq;
nodeDeq.push_back(treeNode);
int index = 2;
while (!dataDeq.empty()){
TreeNode** nodeParent = nodeDeq.front();
nodeDeq.pop_front();
for(int i = 0; i < 2;i++){
data = dataDeq.front();
dataDeq.pop_front();
if(data == nullTreeNodeValue){
if(index%2 == 0) (*nodeParent)->left = nullptr;
else (*nodeParent)->right = nullptr;
}else{
TreeNode * node = new TreeNode();
node->val = data;
if(index%2 == 0){
(*nodeParent)->left = node;
nodeDeq.push_back(&(*nodeParent)->left);
}else{
(*nodeParent)->right = node;
nodeDeq.push_back(&(*nodeParent)->right);
}
}
index++;
}
}
}
class Solution {
public:
int res = 0;
void depth(TreeNode* root, int value){
if(root != nullptr){
value = (value<<1) + root->val;
if(root->left == nullptr && root->right == nullptr){
res += value;
return;
}
depth(root->left, value);
depth(root->right, value);
}
}
void depth2(TreeNode* root, vector<int> path, int deep){
if(root == nullptr) return;
path[deep] = root->val;
if(root->left == nullptr && root->right == nullptr){
for(int d = 0;d <= deep;d++){
res += path[d] * pow(2, deep - d);
}
return;
}
depth2(root->left, path, deep + 1);
depth2(root->right, path, deep + 1);
}
int sumRootToLeaf(TreeNode* root) {
depth(root, 0);
return res;
}
};
int main() {
TreeNode* tree = new TreeNode();
createBiTNode(&tree, treeVec);
Solution s;
cout<<s.sumRootToLeaf(tree);
return 0;
}
|