二叉树算法题

二叉树类定义

public class BinaryNode<T>
{
        public T Element;
        public BinaryNode<T> Left;
        public BinaryNode<T> Right;

        public BinaryNode(T element, BinaryNode<T> left, BinaryNode<T> right)
        {
            this.Element = element;
            this.Left = left;
            this.Right = right;
        }

        public bool IsLeaf()
        {
            if (Left == null && Right == null)
                return true;
            else
                return false;
        }

}

获取二叉树的深度

public int GetDepth(BTreeNode node)
{

        if(node == null)
            return 0;
        else
        {
            int d1=GetDepth(node.LNode);
            int d2=GetDepth(node.RNode);
         }

         return d1>d2?d1++:d2++;

}

public int GetDepthNoRecursive(BTreeNode root)

{

      Queue<int> que = new Queue<int>();

      while(root!=null)
     {

         

      }

}

二叉树广度优先遍历

先序遍历: 先访问节点本身在访问左子节点最后是右子节点. 

 

二叉树的前序遍历:

public void PreOrder(BinaryNode<T> root)
{
           if (root == null)
               return;
           //visit current node
           Console.WriteLine(root.Element);
           PreOrder(root.Left);
           PreOrder(root.Right);
}

二叉树前序遍历 非递归算法:

public void PreOrderNoRecursive(BinaryNode<T> root)
{
           if (root == null)
               return;

           Stack<BinaryNode<T>> stack = new Stack<BinaryNode<T>>();
           while(root !=null || stack.Count > 0)

           {

                 while(root != null){

                          stack.Push(root);

                          Console.WriteLine(root.Element);

                          root = root.Left;

                 }

                 if(stack.Count > 0)

                 {    

                        root = stack.Pop();

                        root = root.Right;

                 }    

           }

}

二叉树中序遍历

InOrder visit

二叉树中序遍历 递归算法

public void InOrder(BinaryNode<T> root)

{

      if(root == null) return;

      InOrder(root.Left);

      Console.WriteLine(root.Element);

      InOrder(root.Right);

}

二叉树中序遍历 非递归算法

public void InOrderNoRecursive(BinaryNode<T> root)

{

      if(root == null)

             return;

      Stack<BinaryNode<T>> stack = new Stack<BinaryNode<T>>();


      while(root!=null || stack.Count>0 )
     {

           while (root != null)

           {

               stack.Push(root);   

               root = root.Left;

            }

             if(stack.Count > 0)

             {

           root = stack.Pop();

           Console.WriteLine(root.Element);

           root = root.Right

             }

      }

}

后序遍历

public void PostOrder(BinaryNode<T> root){

      if(root == null)

            return;

      PostOrder(root.Left);

      PostOrder(root.Right);

}

public void PostOrderNoRecursive(BinaryNode<T> root)

{

        if(root == null)

            return;

        Stack<BinaryNode<T>> stack = new Stack<BinaryNode<T>>();

        while(root != null || stack.Count>0){

               while(root!= null)

               {

                      stack.Push(root);

                      if(root.left != null)

                           root = root.Left;

                      else

                          root = root.right;

                }     

                root = stack.Pop();   

                Console.WriteLine(root.Element); 

                if(stack.Count>0 && root == stack.Peek().Left)

               {

                      root = stack.Peek().Right;

                }

                else

                {

                      root = null;

                 }    

       }
 }

原文地址:https://www.cnblogs.com/stone/p/1877156.html