下面的函数都跑过没问题,传值有问题,最终主函数的node孩子为空...(红色语句出问题,呵呵),达人帮忙看下,不自己想啦~
using System;
using System.Collections.Generic;
using System.Text;namespace Tree
{
class BinaryTreeNode
{
public int value;
public BinaryTreeNode leftchild;
public BinaryTreeNode rightchild; public BinaryTreeNode()
{
this.value = 0;
this.leftchild = null;
this.rightchild = null;
}
} class Program
{
static int[] arr = { 1, 2, 0, 0, 3}; static int i = -1; static void Main(string[] args)
{
BinaryTreeNode node = new BinaryTreeNode();
CreateBinaryTree(node);
Console.WriteLine(node.leftchild.value);
} public static void CreateBinaryTree(BinaryTreeNode p)
{
i = i + 1; if (i < arr.Length)
{
if (arr[i] == 0)
{
p = null;
}
else
{
if (i != 0)
p = new BinaryTreeNode();
p.value = arr[i];
CreateBinaryTree(p.leftchild);
CreateBinaryTree(p.rightchild);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;namespace Tree
{
class BinaryTreeNode
{
public int value;
public BinaryTreeNode leftchild;
public BinaryTreeNode rightchild; public BinaryTreeNode()
{
this.value = 0;
this.leftchild = null;
this.rightchild = null;
}
} class Program
{
static int[] arr = { 1, 2, 0, 0, 3}; static int i = -1; static void Main(string[] args)
{
BinaryTreeNode node = new BinaryTreeNode();
CreateBinaryTree(node);
Console.WriteLine(node.leftchild.value);
} public static void CreateBinaryTree(BinaryTreeNode p)
{
i = i + 1; if (i < arr.Length)
{
if (arr[i] == 0)
{
p = null;
}
else
{
if (i != 0)
p = new BinaryTreeNode();
p.value = arr[i];
CreateBinaryTree(p.leftchild);
CreateBinaryTree(p.rightchild);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;namespace Tree
{
class BinaryTreeNode
{
public int value; public BinaryTreeNode leftchild;
public BinaryTreeNode rightchild; public BinaryTreeNode()
{
this.value = 0; }
} class Program
{
static int[] arr = { 1, 2, 0, 0, 3}; static int i = -1; static void Main(string[] args)
{
BinaryTreeNode node = new BinaryTreeNode(); CreateBinaryTree(ref node); Console.WriteLine(node.leftchild.value);
} public static void CreateBinaryTree(ref BinaryTreeNode p)
{
i = i + 1; if (i < arr.Length)
{
if (arr[i] == 0)
{
p = null;
}
else
{
if (i != 0)
p = new BinaryTreeNode();
p.value = arr[i];
CreateBinaryTree(ref p.leftchild);
CreateBinaryTree(ref p.rightchild);
}
}
}
}
}