Skip to content

Added a new Program with the best Execution Time #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Medium/Maximum Difference Between Node and Ancestor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int res=Integer.MIN_VALUE;
int max(TreeNode root)
{
if(root==null) return Integer.MIN_VALUE;
if(root.left==null && root.right==null) return root.val;
int l=max(root.left);
int r=max(root.right);
int m=Math.max(l,r);
res=Math.max(res,Math.abs(root.val-m));
return Math.max(root.val,m);
}
int min(TreeNode root)
{
if(root==null) return Integer.MAX_VALUE;
if(root.left==null && root.right==null) return root.val;
int l=min(root.left);
int r=min(root.right);
int m=Math.min(l,r);
res=Math.max(res,Math.abs(root.val-m));
return Math.min(root.val,m);
}
public int maxAncestorDiff(TreeNode root) {
max(root);
min(root);
return res;
}
}