c++ - C++ 中的二叉树基础知识

标签 c++ binary-tree

我有一个二叉树数据结构:

//Declare Data Structure
struct CP {
    int id;         //ID of the Node
    int data;       //Data of the Node
    CP * left;      //Pointer to the Left Subtree 
    CP * right;     //Pointer to the Right Subtree
};

typedef CP * CPPtr; 

在不更改树结构的情况下,如果给定节点 ID,我如何实际计算深度。 (id是每个树节点的唯一标识)

最佳答案

您的代码缺少一些基本步骤或必要的初始化。

 BTree_Helper(BTree *Tree){// this is roughly written like pseudo code
    if(TLeft == NULL && TRight == NULL){
      depth of tree = 0 ;       
    }
    else if (TLeft == NULL){
      depth of tree = depth of right tree ;       
    }
    else if(TRight==NULL){
      depth of tree = depth of left tree;        
    }
    else{
      depth of tree = the maximum between depth of left and depth of right;
    }
 }

为了您的方便,我只是给出了一些提示。 仔细考虑并尝试尽可能多的测试套件。

关于c++ - C++ 中的二叉树基础知识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9677311/

相关文章:

c++ - 二叉树可视化 - 循环树? (QT)

algorithm - 根据 morris inorder,这棵树的下一步是什么?

java - 为什么我的 BST 不会写入文件?

c - C中二叉树的中序树遍历

java - 具有二元表达式树的访问者设计模式

c++ - 这个简单的 c++ 程序永远运行,我不明白为什么

c++ - 优先选择一个 operator[] 而不是另一个

C++ 难以在单例类中创建类的实例

c++ - 访问外部 float 组时遇到问题

c++ - 我在 Qt Creator 中不断收到 "Undefined reference to WinMain@16"