Я пишу код для удаления узла с val 1. Я реализовал для него две функции. оба кажутся мне одинаковыми, но дают другой результат. пожалуйста, кто-нибудь может указать, почему это происходит именно так? код: -
Две функции:
- node deleteNodeWithVal1Funct1 (node root)
- void deleteNodeWithVal1Funct2 (node root)
publi c class Doubt1 {
class node
{
int data;
node left, right;
public node()
{
this.data = 0;
left=right=null;
}
public node(int data)
{
this.data = data;
left=right=null;
}
} ;
public static void printTreeInoreder(node root)
{
if(root==null)
return;
printTreeInoreder(root.left);
System.out.print(" "+root.data);
printTreeInoreder(root.right);
}
public static node deleteNodeWithVal1Funct1(node root)
{
if(root==null)
return null;
if(root.data==1)
return null;
root.left=deleteNodeWithVal1Funct1(root.left);
root.right=deleteNodeWithVal1Funct1(root.right);
return root;
}
public static void deleteNodeWithVal1Funct2(node root)
{
if(root==null)
return ;
if(root.data==1)
{
root=null;
return;
}
deleteNodeWithVal1Funct2(root.left);
deleteNodeWithVal1Funct2(root.right);
}
public static void main(String s[])
{
Doubt1 ob=new Doubt1();
node tree=ob.new node(0); // 0
tree.left=ob.new node(1); // -> / \
tree.right=ob.new node(2); // 1 2
printTreeInoreder(tree); // gives 1 0 2
deleteNodeWithVal1Funct2(tree);
printTreeInoreder(tree); // gives 1 0 2 why ?why not 02 ?
deleteNodeWithVal1Funct1(tree);
printTreeInoreder(tree); // gives 0 2 why? whynot 1 0 2 ?
}
}