C#二叉树生成和遍历以及计算最大宽度(包括null)

  1. leetcode的662. 二叉树最大宽度题目参考。
  2. 二叉树的建立与遍历。

基于引用的思路,实际可以再优化,这里就不多写了。

这部分是生成和遍历。

        class Tree
        {
            public TreeNode root;
            Stack<TreeNode> stack = new Stack<TreeNode>();

            public void CreateTree(string description)
            {
                bool left = true;
                char[] descriptionarray = description.ToCharArray();

                root = new TreeNode();
                root.val = descriptionarray[0];
                TreeNode temp = root;

                for (int i = 1; i <= descriptionarray.Length - 1; i++)
                {

                    if (descriptionarray[i] == '(')
                    {
                        left = true; stack.Push(temp);
                    }
                    else if (descriptionarray[i] == ',')
                    {
                        left = false;
                    }
                    else if (descriptionarray[i] == ')')
                    {
                        stack.Pop();
                    }
                    else
                    {
                        if(descriptionarray[i]=='n')
                        {
                            temp = null;
                        }
                        else
                        {
                            temp = new TreeNode();
                            temp.val = descriptionarray[i];
                        }
                        if (left == true)
                        {
                            stack.Peek().left = temp;
                        }
                        else
                        {
                            stack.Peek().right = temp;
                        }
                    }

                }
            }
            public void PreOrder(TreeNode t, String sign)
            {

                if (t != null)
                {
                    Console.WriteLine(sign + t.val);
                    sign += sign;
                    if (t.left != null)
                    {

                        PreOrder(t.left, sign);
                    }
                    if (t.right != null)
                    {

                        PreOrder(t.right, sign);
                    }
                }
            }
        }

TreeNode类

        public class TreeNode
        {
            public char val;
            public TreeNode left;
            public TreeNode right;
            public TreeNode(char x, TreeNode l, TreeNode r)
            {
                val = x;
                left = l;
                right = r;
            }
            public TreeNode()
            {
                left = right = null;
            }
        }

记录结点数

        public class MyNode
        {
            public TreeNode node;
            public int coordinate;
            public MyNode(TreeNode node, int coordinate) { this.node = node; this.coordinate = coordinate; }
        }

计算最大宽度

        public class Solution
        {
            public int WidthOfBinaryTree(TreeNode root)
            {
                int result = 0;
                if (root == null)
                {
                    return result;
                }
                result = 1;
                Queue<MyNode> queue = new Queue<MyNode>();
                queue.Enqueue(new MyNode(root, 1));
                while (queue.Count > 0)
                {
                    int count = queue.Count;
                    var list = new List<MyNode>();
                    for (int i = 0; i < count; i++)
                    {
                        list.Add(queue.Dequeue());
                    }

                    for (int i = 0; i < count; i++)
                    {
                        if (list[i].node.left != null)
                        {
                            queue.Enqueue(new MyNode(list[i].node.left, list[i].coordinate * 2));
                        }
                        if (list[i].node.right != null)
                        {
                            queue.Enqueue(new MyNode(list[i].node.right, list[i].coordinate * 2 + 1));
                        }
                    }

                    result = Math.Max(result, list.Last().coordinate - list.First().coordinate + 1);
                }

                return result;
            }
        }

运行

  static void Main(string[] args)
  {
  			string x = "1(3(5,3),2(n,9))";
            Tree t = new Tree();
            t.CreateTree(x);
            Solution solution = new Solution();
            Console.WriteLine("二叉树宽度"+solution.WidthOfBinaryTree(t.root));
  }

结果是4

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值