惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - stone

release 版本dll的调试 dead lock in thread pool Excel C# Automation 如何让iframe 自动适应窗口的高度 自定义Silverlight toolkit 里面的 Column Chart 的data point How to do live debug the Managed code in Windows Phone 7 单链表操作相关算法 BUG: "Old format or invalid type library" error when automating Excel on 64 bit server 2008 - stone enable Assembly Load Trace 不合法的XML字符必须被替换为相应的实体 - stone - 博客园 如何修改 VS 自动生成的 COM interop dll VS中Sos调试扩展简介 (转帖) Sql server 2005 connection string - stone Get depth of BTree Quick sort C# code(2) Quick sort C# code use the network trace, from msdn. - stone 字节流编码获取原来这么复杂,但也很简单 通过DataTable获得表的主键 让IE支持自己的协议
二叉树算法题
stone · 2010-11-14 · via 博客园 - stone

二叉树类定义

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;

                 }    

       }
 }