Cppツリー

3557 ワード

#include
#include
using namespace std;


//           
struct BinaryTreeNode { 
    int val;
    BinaryTreeNode *left;
    BinaryTreeNode *right;
    BinaryTreeNode(int x) : val(x), left(NULL), right(NULL) {}
};


//             
struct BinaryTreeNode *CreatBinaryTree(){
    int a;
    cin >> a; //          2   0          
    if(a == 0) return NULL;

    struct BinaryTreeNode *newnode = (struct BinaryTreeNode*)malloc( sizeof(struct BinaryTreeNode) );
    newnode->val = a;
    newnode->left  = CreatBinaryTree();  //       
    newnode->right = CreatBinaryTree();  //       
    return newnode;
}


//    
void PreOrderTraverse(struct BinaryTreeNode *root){
    if(root){
        cout << root->val << ' ';
        PreOrderTraverse(root->left);
        PreOrderTraverse(root->right);
    }
}


//    
void InOrderTraverse(struct BinaryTreeNode *root){  
    if(root){
        InOrderTraverse(root->left);
        cout << root->val << ' ';
        InOrderTraverse(root->right);
    }
}


//    
void LastOrderTraverse(struct BinaryTreeNode *root){
    if(root){
        LastOrderTraverse(root->left);
        LastOrderTraverse(root->right);
        cout << root->val << ' ';
    }
}


//    ,    ,        
void LevelOrderTraverse(BinaryTreeNode* root, int depth, vector> &result) {
    if(root==NULL) return;
    if(depth >= result.size()) result.push_back(vector{});
    result[depth].push_back(root->val);
    LevelOrderTraverse(root->left,  depth + 1, result);
    LevelOrderTraverse(root->right, depth + 1, result);
}
void LevelOrderTraverse(BinaryTreeNode* root) {
    vector> result;
    LevelOrderTraverse(root, 0, result);
    for(auto line:result){
        for(auto e:line) cout<left)+NodeNum(root->right);
    else
        return 0;
}


//        
int LeafNum(struct BinaryTreeNode *root){
    if(!root)
        return 0;
    else if( (root->left == NULL) && (root->right == NULL) )
        return 1;
    else
        return LeafNum(root->left) + LeafNum(root->right);
}


//       
int maxDepth(struct BinaryTreeNode *root){
    if(root == NULL) return 0;
    return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}


//       (           ,     )
int minDepth(struct BinaryTreeNode *root){  
    if(root == NULL) return 0;
    if(root->left == NULL) return minDepth(root->right) + 1;
    if(root->right == NULL) return minDepth(root->left) + 1;
    return min(minDepth(root->left), minDepth(root->right)) + 1;
}


//       
void binaryTreePaths(BinaryTreeNode* node,string path,vector &result){
    path += to_string(node->val);
    //       ,
    if(node->left==NULL && node->right==NULL) result.push_back(path);
    if(node->left!=NULL) binaryTreePaths(node->left, path + "->",result);
    if(node->right!=NULL) binaryTreePaths(node->right,path + "->",result);
} 
vector binaryTreePaths(BinaryTreeNode* root) {
    vector result;
    if(root!=NULL) binaryTreePaths(root,"",result);
    return result;
}



int main(){
    struct BinaryTreeNode *root;
    
    root = CreatBinaryTree();
    
    cout< allPaths = binaryTreePaths(root);
    for(auto e:allPaths) cout <