代码改变世界

算法——树(10%)

2012-12-18 15:13  msfte  阅读(294)  评论(0编辑  收藏  举报

 1,把二元查找树转变成排序的双向链表(树)

2,在二元树中找出和为某一值的所有路径(树)

3, 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。

4, 求二叉树中节点的最大距离

5, 输入一颗二元查找树,将该树转换为它的镜像,

即在转换后的二元查找树中,左子树的结点都大于右子树的结点。

用递归和循环两种方法完成树的镜像转换。

例如输入:

8

/\

6 10

/\/\

5 7 9 11

输出:

8

/ \

10 6

/\/\

11 9 7 5

        static void frontOrderMirror(Node root)
        {
            if (root == null) return;

            Node p;

            p = root.LeftChild;
            root.LeftChild = root.RightChild;
            root.RightChild = p;

            frontOrderMirror(root.LeftChild);
            frontOrderMirror(root.RightChild);

        }    

 非递归方法:

        static void frontOrderMirrorNoRecur(Node root)
        {
            Stack<Node> cache = new Stack<Node>();
            Node p = root;

            while (cache.Count != 0 || p != null)
            {
                while (p != null)
                {
                    Node q;

                    q = p.LeftChild;
                    p.LeftChild = p.RightChild;
                    p.RightChild = q;

                    cache.Push(p);
                    p = p.LeftChild;
                }

                if (cache.Count != 0)
                {
                    p = cache.Pop();
                    p = p.RightChild;
                }
            }
        }    


 

 

6,层序遍历二叉树

 

 static int GetDeep(Node root) {
            if (root == null) return 0;

            int leftDeep = GetDeep(root.LeftChild);
            int rightDeep = GetDeep(root.RightChild);

            int maxDeep = (leftDeep > rightDeep ? leftDeep : rightDeep)+1;

            return maxDeep;
             

        }

        static void OneLevelTravel(Node root,int level)
        {
            if (root == null) return;

            if(level == 1)
                Console.WriteLine("{0}", root.Value);

            OneLevelTravel(root.LeftChild, level-1);
            OneLevelTravel(root.RightChild, level-1);      
        }


        static void AllOneLevelTravel(Node root, int level)
        {
            for (int i = 1; i <= level; i++) {
                OneLevelTravel(root, i);
            }
            
        
        }

 

7, 递归和非递归俩种方法实现二叉树的后序遍历。

        static void FollowOrderNoRecur(Node root)
        {
            Stack<Node> cache = new Stack<Node>();
            Node p = root;

            while (cache.Count != 0 || p != null)
            {
                while (p != null)
                {
                    cache.Push(p);
                    p = p.LeftChild;
                }

                if (cache.Count != 0)
                {
                    p = cache.Pop();

                    if (p.Flag == 1)
                    {
                        cache.Push(p);
                        p.Flag = 0;
                        p = p.RightChild;               
                    }
                    else
                    {
                        Console.WriteLine("{0}", p.Value);
                        p = null;
                    }                  
                }           
            }
        }   

 

 

 

 

8, 输入一棵二元树的根结点,求该树的深度。

从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。

9, 二叉树两个结点的最低共同父结点(树)

    class Node
        {
         private int flag = 1;
         public  Node LeftChild  { get; set;}
         public  Node RightChild { get; set;}
         public  char Value{get;set;}
         public  int  Flag { get{return flag ;} set{flag = value;} }
        }


        class LinkNode
        {

            public LinkNode Next{ get; set; }
            public char Value { get; set; }

        }


        static Node FindCommonParent(Node node1, Node node2 ,Node root)
        {

            if (node1 == null || node2 == null || root == null) return null;


            LinkNode head1 = new LinkNode();
            LinkNode head2 = new LinkNode();

            FindPath(root, node1, head1);
            FindPath(root, node2, head2);


            LinkNode common = FindFirstCommondNode(head1, head2);

            if(common != null){

                return new Node { Value = common.Value };
            
            }

            return null;
        }

        static LinkNode FindFirstCommondNode(LinkNode head1, LinkNode head2)
        {
            int count1 = 0; 
            int count2 = 0;
            LinkNode p = head1;

            while (p != null) {
                count1++;
                p = p.Next;
            }
            p = head2;
            while (p != null)
            {
                count2++;
                p = p.Next;
            }

            int distance =  Math.Abs(count1-count2);

            if (count1 > count2)
            {
                for (int i = 0; i < distance; i++)
                {
                    head1 = head1.Next;
                }

            }
            else {
            
                for (int i = 0; i < distance; i++)
                 {
                     head2 = head2.Next;
                 }   
            }

            while (head1 != null && head2 != null)
            {
                if (head1.Value == head2.Value)
                {
                    return head1;
                }

                head1 = head1.Next;
                head2 = head2.Next;

            }

            return null;
        }

        static LinkNode FindPath(Node root, Node target, LinkNode linkHead)
        {
           
            if (root == null) return null;
            if (target.Value == root.Value) {

                linkHead.Value = root.Value;
                return linkHead;            
            }

            LinkNode p =null;
            p = FindPath(root.LeftChild, target, linkHead);

            if (p == null)
                p = FindPath(root.RightChild, target, linkHead);

            if (p != null) {
                p.Next = new LinkNode {Value = root.Value };
                return p.Next;
            }
            return null;
        }

  

 

 

10, 怎样编写一个程序,把一个有序整数数组放到二叉树中?

11,判断一个数字序列是BST后序遍历的结果

12,给一个二叉树,每个节点都是正或负整数,如何找到一个子树,它所有节点的和最大? 

13,有一个排序二叉树,数据类型是int型,如何找出中间大的元素

14,二叉树查找不严格小于一个值的最大值(返回节点)。
15,中序遍历二叉树,结果为ABCDEFGH,后序遍历结果为ABEDCHGF,那么前序遍历结果为

 

            static void CreatTreeByMidandFollow(Node root, int MidStartIndex, int MidEndIndex, int FollowStartIndex,int FollowEndIndex,char[] mid, char[] follow) {


            int rootInMidIndex = 0;

            for (int i = MidStartIndex; i <= MidEndIndex; i++)
            {

                if (mid[i] == follow[FollowEndIndex])
                {
                    rootInMidIndex = i;
                    break;
                }
                
            }

            root.Value = follow[FollowEndIndex];

            int rightLength = MidEndIndex - rootInMidIndex;
            int leftLength = MidEndIndex - MidStartIndex - rightLength;


            

            if (leftLength > 0) {

                int leftRootIndex = FollowStartIndex + leftLength-1;
                root.LeftChild = new Node();

                CreatTreeByMidandFollow(root.LeftChild, MidStartIndex, rootInMidIndex - 1, FollowStartIndex, leftRootIndex, mid, follow);
            }

            if (rightLength > 0) {

                int rightRootIndex = FollowEndIndex - 1;
                root.RightChild = new Node();

                CreatTreeByMidandFollow(root.RightChild, rootInMidIndex+1, MidEndIndex, FollowStartIndex + leftLength , rightRootIndex, mid, follow);

            }
  
            
        }

 

16,对于一个多叉树,设计TreeNode节点和函数,返回先序遍历情况下的下一个节点。

17,将一个普通的二叉树转换为二叉排序树

18,给定一棵树根节点,每个节点里面的值都不相同,查找iKEY的节点,并使用一个给定的节点将查找到的节点替换掉。节点内有两个孩子节点和一个父节点。

19,如何删除一个搜索二叉树的结点

20, 四种遍历,递归非递归

23,实现完整二叉查找树(Init,Search,Insert,Update,Delete,Length)