zoukankan      html  css  js  c++  java
  • 判断平衡二叉树

    /*
    	判断平衡二叉树
    	平衡二叉树特点:
    		左右子树高度差之多为1(<=1) 
    */ 
    
    #include <iostream>
    
    using namespace std;
    
    struct TreeNode
    {
    	int val;
    	struct TreeNode *L;
    	struct TreeNode *R;
    	
    	TreeNode(int x):
    	val(x),L(NULL),R(NULL){}
    };
    
    class  Balance
    {
    	public:
    	/*
    		Tree Depth 
    	*/
    	int Depth(TreeNode *root)
    	{
    		if(root==NULL)
    		{
    			return 0;
    		}
    		int LeftDepth=Depth(root->L)+1;
    		int RightDepth=Depth(root->R)+1;
    		return((LeftDepth>RightDepth)?LeftDepth:RightDepth);
    	}
    	
    	bool isBalance(TreeNode *root)
    	{
    		int LD=Depth(root->L);
    		int RD=Depth(root->R);
    		if(abs(LD-RD)>1)
    		{
    			false;
    		}	
    		return true;
    	}
    }
    
    int main(void)
    {
    	
    }
    

      

  • 相关阅读:
    WCF中NetTCp配置
    生产者消费者模式
    MVC 引擎优化
    Wcf
    MongoDB运用
    Sock基础
    WebService
    线程
    委托
    特性
  • 原文地址:https://www.cnblogs.com/achao123456/p/7568970.html
Copyright © 2011-2022 走看看