• 二叉树算法题


    二叉树类定义

    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;

                     }    

           }
     }

  • 相关阅读:
    移动端测试作业小集合 (6)
    移动端测试——手机常见操作的API (5)
    移动端测试——APP元素信息、事件操作、模拟手势API(4)
    移动端测试——APP元素定位操作 (3)
    移动端测试——App基础操作(2)
    移动端测试基础 (1)
    Python进阶-一切皆对象及type-object-class间的关系
    故障-解决pip安装mysqlclient、gevent报找不到cc或gcc错误问题
    Linux环境上部署Flask
    解决多版本共存时,python/pip等命令失效
  • 原文地址:https://www.cnblogs.com/stone/p/1877156.html
Copyright © 2020-2023  润新知