zoukankan      html  css  js  c++  java
  • 面试题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;
    }
    

  • 相关阅读:
    C语言寒假大作战01
    C语言I作业12—学期总结
    C语言I博客作业11
    C语言I博客作业10
    C语言I博客作业09
    C语言I博客作业08
    计算机组成与设计 复习
    概率论与数理统计 期末复习
    SPM(Software Project Management)课程感想
    Software Project Management_JUnit && Maven
  • 原文地址:https://www.cnblogs.com/robbychan/p/3787167.html
Copyright © 2011-2022 走看看