bool isBalanced(TreeNode* root) {
     if(root == NULL) return true;
     int r1 = treeDepth(root->left);
     int r2 = treeDepth(root->right);
     if(r1 == -1 || r2 == -1) return false;
     return abs(r1-r2)<=1? true: false;
 }
	bool isBalanced(TreeNode* root) {
		if (!root) {
			return true;
		}
		if (abs(treeDepth(root->left) - treeDepth(root->right)) <= 1) {
			return isBalanced(root->left) && isBalanced(root->right);
		}
		return false;
	}
示例#3
0
size_t treeDepth(IntTree** tree)
{
    if (NULL==tree) {
        throw 0;
    } else if (NULL==*tree) {
        return 0;
    } else {
        return 1+std::max(treeDepth(&(*tree)->left), treeDepth(&(*tree)->right));
    }
}
 int treeDepth(TreeNode* root) {
     if(root == NULL) return 0;
     int r1 = 0;
     int r2 = 0;
     if(root->left != NULL) r1 = treeDepth(root->left);
     if(root->right != NULL) r2 = treeDepth(root->right);
     if(r1 == -1 || r2 == -1)  return -1;
     if(abs(r1-r2)>1) return -1;
     return r1>r2? r1+1: r2+1;
 }
int treeDepth(TreeNode n)
{
	if (n == NULL){
		return 0;
	}

	else{
		int ldepth = treeDepth(n->leftnode);
		int rdepth = treeDepth(n->rightnode);
		return 1 + ( (ldepth > rdepth) ? ldepth : rdepth );
	}
}
示例#6
0
/********************** 参考代码 ***********/
int treeDepth(BinaryTreeNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	
	int leftDepth = treeDepth(root->left);
	int rightDepth = treeDepth(root->right);
	
	return (leftDepth > rightDepth) ? (leftDepth + 1) : (rightDepth + 1);
}
示例#7
0
void calculateForce(cellptr root, double eps, real rootSize)
{
	/* List of nodes to be calculate */
	nodeptr * activeList;
	/* List of interacting nodes */
	cellptr interactionList;
	/* Maximum number of active nodes */
	int activeMaxCount;
	/* Center of root */
	vector rMid;
	/* */
	/* Estimate list length     */
	activeMaxCount = 216 * (treeDepth(root) + 1);

	activeList = (nodeptr*) malloc(activeMaxCount * sizeof(nodeptr));
	interactionList = (cellptr) malloc(activeMaxCount * sizeof(cell));

	/* Initialize active list */
	activeList[0] = (nodeptr) root;
	/* Initialize center of root */
	CLRV(rMid)
	/* Start walking the tree */
	walkTree(activeList, activeList + 1,
		interactionList, interactionList + activeMaxCount,
		(nodeptr) root, rootSize, rMid,
		eps);
	/* Cleaning up */
	free(activeList);
	free(interactionList);
}
示例#8
0
int main() {
    g_counter=0;
    IntTree* binaryTree;
    buildIntTree(Path(), &binaryTree);
    std::cout << "Depth: " << treeDepth(&binaryTree) << std::endl;
    dfs(&binaryTree);
}
void MainWindow::treeDepth(QModelIndex currentIndex,unsigned int* depth,unsigned int tempDepth) {
    tempDepth++;
    int count = 0;
    for (unsigned int i = 0; i < projectsTreeModel->rowCount(currentIndex); i++) {
        count ++;
        treeDepth(currentIndex.child(i,0),depth,tempDepth);
    }
    if (count==0 && tempDepth>*depth) *depth=tempDepth;

}
示例#10
0
int treeDepthRec(cellptr node)
{
	int i, l = 0;
	nodeptr q;
	for (i = 0; i < NCHILD; i++) {
		if ((q = Child(node)[i]) != NULL)
			/* If node is a cell */
			if (Type(q) == BODY) {
				l = MAX(l, 0);
			} else {
				l = MAX(l, 1 + treeDepth((cellptr) q));
			}
	}
	return l;
}
示例#11
0
文件: test.c 项目: xsun89/CZBKStudy
void main()
{
	BTreeNode b1, b2, b3, b4, b5;
	memset(&b1, 0, sizeof(BTreeNode));
	memset(&b2, 0, sizeof(BTreeNode));
	memset(&b3, 0, sizeof(BTreeNode));
	memset(&b4, 0, sizeof(BTreeNode));
	memset(&b5, 0, sizeof(BTreeNode));
	b1.data = 1;
	b2.data = 2;
	b3.data = 3;
	b4.data = 4;
	b5.data = 5;

	b1.lptr = &b2;
	b1.rptr = &b3;
	b2.lptr = &b4;
	b3.lptr = &b5;
	printf("\n preorder");
	preOrder(&b1);
	printf("\n inOrderEx");
	inOrderEx(&b1);

	printf("\n inOrder");
	inOrder(&b1);

	printf("\n postOrder");
	postOrder(&b1);
	
	int depth = treeDepth(&b1);

	printf("\n tree depth=%d", depth);
	BTree co = treeCpoy(&b1);
	preOrder(co);

	printf("\n");
	system("pause");
}
	int treeDepth(TreeNode* root) {
		if (!root) {
			return 0;
		}
		return max(treeDepth(root->left), treeDepth(root->right)) + 1;
	}
示例#13
0
文件: 39.cpp 项目: JiangQH/algorithms
int treeDepth(TreeNode* node) {
	if (!node)
		return 0;
	return max(treeDepth(node->left), treeDepth(node->right)) + 1;
}
示例#14
0
bool isSymmetric(TreeNode* root) {
    if (root == nullptr || (root->left == nullptr && root->right == nullptr))
        return true;
    if ((root->left != nullptr && root->right == nullptr)
        || (root->left == nullptr && root->right != nullptr)
        || (root->left->val != root->right->val))
        return false;

    std::function<int(TreeNode* root)>
        treeDepth = [&](TreeNode* root)->int
    {
        if (root == nullptr)
            return 0;
        if (root->left == nullptr && root->right == nullptr)
            return 1;
        return std::max(treeDepth(root->left), treeDepth(root->right)) + 1;
    };

    int loopTime = treeDepth(root) - 2;

    std::vector<TreeNode*> nodeVec;
    nodeVec.push_back(root->left);
    nodeVec.push_back(root->right);
    int len = 2;
    for (int count = 0; count < loopTime; ++count)
    {
        for (int i = 0, j = len - 1; i < j; ++i, --j)
        {
            if (nodeVec[i] == nullptr && nodeVec[j] == nullptr)
                continue;

            if ((nodeVec[i]->left == nullptr && nodeVec[j]->right == nullptr)
                || ((nodeVec[i]->left != nullptr && nodeVec[j]->right != nullptr)
                    && (nodeVec[i]->left->val == nodeVec[j]->right->val)))
            {
                if ((nodeVec[i]->right == nullptr && nodeVec[j]->left == nullptr)
                    || ((nodeVec[i]->right != nullptr && nodeVec[j]->left != nullptr)
                        && (nodeVec[i]->right->val == nodeVec[j]->left->val)))
                    ;
                else
                    return false;
            }
            else
                return false;
        }

        int iEnd = nodeVec.size();
        for (int i = 0; i < iEnd; ++i)
        {
            if (nodeVec[i] == nullptr)
            {
                nodeVec.push_back(nullptr);
                nodeVec.push_back(nullptr);
            }
            else
            {
                nodeVec.push_back(nodeVec[i]->left);
                nodeVec.push_back(nodeVec[i]->right);
            }
        }
        for (int i = 0; i < iEnd; ++i)
            nodeVec.erase(nodeVec.begin());
        len *= 2;
    }
    return true;
}