leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1022.cpp (610B)


0 class Solution {
1 public:
2 int sumRootToLeaf(TreeNode *root) {
3 if (!root) return 0;
4 stack<pair<TreeNode *, int>> st;
5 int sum = 0;
7 st.push({root, 0});
8 while (!st.empty()) {
9 TreeNode *root = st.top().first;
10 int val = (st.top().second << 1) | root->val;
11 st.pop();
12 if (!root->left && !root->right) {
13 sum += val;
14 continue;
15 }
16 if (root->left) st.push({root->left, val});
17 if (root->right) st.push({root->right, val});
18 }
19 return sum;
20 }
21 };