Beispiel #1
0
 public static int Height(Week7.Assignments.Node root)
 {
     if (root == null)
     {
         return(-1);
     }
     return(Math.Max(Height(root.Left), Height(root.Right)) + 1);
 }
Beispiel #2
0
 public bool IsBST(Week7.Assignments.Node root, int min, int max)
 {
     if (root == null)
     {
         return(true);
     }
     if (root.Data < min || root.Data > max)
     {
         return(false);
     }
     return(IsBST(root.Left, min, root.Data) && IsBST(root.Right, root.Data, max));
 }
Beispiel #3
0
        public static int Diameter(Week7.Assignments.Node root)
        {
            if (root == null)
            {
                return(0);
            }

            int lh = Height(root.Left);
            int rh = Height(root.Right);

            int ld = Diameter(root.Left);
            int rd = Diameter(root.Right);

            return(Math.Max(lh + rh + 1, Math.Max(ld, rd)));
        }
Beispiel #4
0
        public static Node TreeToLList(Week7.Assignments.Node root, Node hd, LinkedList <Node> list)
        {
            if (root == null)
            {
                return(hd);
            }

            TreeToLList(root.Left, hd, list);
            hd.Next = new Node {
                Data = root.Data
            };
            hd = hd.Next;
            list.AddLast(hd);
            TreeToLList(root.Right, hd, list);

            return(head);
        }