• 面试题09 从二叉树的深度扩展到判断是否是二叉平衡树 【树】 Dserving thinking


    判断二叉树是否是平衡二叉树,二叉树深度
    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <vector>
    #include <stack>
    #include <deque>
    #include <queue>
    #include <bitset>
    #include <list>
    #include <map>
    #include <set>
    #include <iterator>
    #include <algorithm>
    #include <functional>
    #include <utility>
    #include <sstream>
    #include <climits>
    #include <cassert>
    #define BUG puts("here!!!");
    
    using namespace std;
    struct Node {
    	int value;
    	Node *lchild, *rchild;
    };
    int treeDep(Node* root) {
    	if(root == NULL) return 0;
    	int nLeft = treeDep(root->lchild);
    	int nRight = treeDep(root->rchild);
    	return (nLeft > nRight ? nLeft : nRight) + 1;
    }
    bool isBalance(Node* root) {
    	if(root == NULL) return true;
    	int left, right;
    	left = treeDep(root->lchild);
    	right = treeDep(root->rchild);
    	int diff = left - right;
    	if(diff < -1 || diff > 1) {
    		return false;
    	}
    	return isBalance(root->lchild) && isBalance(root->rchild);
    }
    bool isBalance(Node* root, int *dep) { // 地址传递
    	if(root == NULL) {
    		*dep = 0;
    		return true;
    	}
    	else {
    		int left, right;
    		if(isBalance(root->lchild, &left) && isBalance(root->rchild, &right)) {
    			int diff = left - right;
    			if(diff >= -1 || diff <= 1) {
    				*dep = (left > right ? left : right) + 1;
    				return true;
    			}
    		}
    		return false;
    	}
    }
    int main() {
    	return 0;
    }
    

  • 相关阅读:
    jsonview插件的常见使用方法整理
    有哪些可以节省chrome内存的扩展插件?
    js得到时间戳(10位数)
    html模板引擎jade的使用
    js获取url参数,操作url参数
    追加window.onload函数
    解决jquery与zepto等其它库冲突兼容的问题
    centos 搭建web平台
    简易web服务器(npm)
    js函数调用与声明 (for时注意)
  • 原文地址:https://www.cnblogs.com/robbychan/p/3787167.html
Copyright © 2020-2023  润新知