算法-二叉树的构造
namespace Binary; public class BinaryTree{ public Node<char> Head{ get; private set; } private string cStr{get;set;} public BinaryTree(string constructStr) { this.cStr = constructStr; this.Head = new Node<char>(constructStr[0]); BuildeTree(Head,0); } private void BuildeTree(Node<char> head,int index){ if(head == null) return; var left = 2*index + 1; var right = 2*index + 2; if(left < cStr.Length) {
if(cStr[left] != '#')
{
head.Left = new Node<char>(cStr[left]);
BuildeTree(head.Left,left);
}
} if(right < cStr.Length) { head.Right = new Node<char>(cStr[right]); BuildeTree(head.Right,right); } } public class Node<T>{ public Node<T>? Left{get;set;} public Node<T>? Right{get;set;} public T Val{get;set;} public Node(T val) { this.Val = val; Left = null; Right = null; } } }