-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #578 from Akansha77/Akansha_75
Day22 q2: Sum of Left Leaves #464 , Day18 q1: max difference between Node and Ancestor .
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
Day-18/q1: Maximum Difference Between Node and Ancestor/Akansha--C.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* 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) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
int maxAncestorDiff(TreeNode* root) { | ||
int m = 0; | ||
dfs(root, m); | ||
return m; | ||
} | ||
|
||
private: | ||
std::pair<int, int> dfs(TreeNode* root, int& m) { | ||
if (root == nullptr) { | ||
return {INT_MAX, INT_MIN}; | ||
} | ||
|
||
auto left = dfs(root->left, m); | ||
auto right = dfs(root->right, m); | ||
|
||
int minVal = std::min(root->val, std::min(left.first, right.first)); | ||
int maxVal = std::max(root->val, std::max(left.second, right.second)); | ||
|
||
m = std::max({m, std::abs(minVal - root->val), std::abs(maxVal - root->val)}); | ||
|
||
return {minVal, maxVal}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* 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) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
void solve(TreeNode*root,int &sum,int &flag){ | ||
if(root==NULL)return ; | ||
if(root->left==NULL && root->right==NULL &&flag==1){ | ||
sum+=root->val; | ||
} | ||
flag=1; | ||
solve(root->left,sum,flag); | ||
flag=0; | ||
solve(root->right,sum,flag); | ||
|
||
} | ||
public: | ||
int sumOfLeftLeaves(TreeNode* root) { | ||
int sum=0; | ||
int flag=0; | ||
solve(root,sum,flag); | ||
return sum; | ||
} | ||
}; |