Skip to content
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

Add files via upload #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
66 changes: 66 additions & 0 deletions slides/binarysearchtree/SumofparentNodes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
public class SumofparentNodes {
static int sum=0;
static class Node
{
int data;
Node left=null;
Node right=null;



};
public static Node NewNode(int n)
{
Node new_node=new Node();
new_node.data=n;
new_node.left=null;
new_node.right=null;
return new_node;
}

public static void parentnodesum(Node root,int x)
{

if(root==null)
{
return;
}
if(root.left!=null && root.left.data==x || root.right!=null && root.right.data==x)
{
sum=sum+root.data;

}

parentnodesum(root.left,x);

parentnodesum(root.right,x);
System.out.println(sum);


}
static int sumOfParentOfXUtil(Node root,
int x)
{
sum=0;
parentnodesum(root, x);

// required sum of parent nodes
return sum;
}
public static void main(String args [])
{
Node root=NewNode(4);
root.left= NewNode(2);
root.right=NewNode(5);
root.left.left=NewNode(7);
root.left.right=NewNode(2);
root.right.left=NewNode(2);
root.right.right=NewNode(3);
int x=2;

int result=sumOfParentOfXUtil(root,x);
System.out.println(result);


}
}