java树转化为二叉树

作者在 2007-06-21 04:29:00 发布以下内容

有网友问我树怎么转化为二叉树,我由于忙于考试没来得及写,但最后不小心在网上看到了这个得法,很好的

package Test;

public class BinaryTreeNode
{
 BinaryTreeNode leftChild;
 BinaryTreeNode rightChild;
 int ;
}

class TreeNode
{
 TreeNode child[];
 int child_count;
 int ;

 BinaryTreeNode ToBinaryTree(TreeNode root)
 {
  if(root == null)
   return null;
  BinaryTreeNode binaryRoot = new BinaryTreeNode();
  binaryRoot. = root.;
  binaryRoot.leftChild = ToBinaryTree(root.child[0]);
  BinaryTreeNode brother = binaryRoot.leftChild;
  for(int i = 1; i < root.child_count;i++)
  {
   brother.rightChild = ToBinaryTree(root.child);
   brother = brother.rightChild;
  }
  return binaryRoot;
 }
}

java算法设计 | 阅读 2273 次
文章评论,共0条
游客请输入验证码