zoukankan      html  css  js  c++  java
  • 二叉树的部分操作(递归)

    二叉树的部分操作。前序、中序、后序遍历,节点个数、k层节点个数、查找、叶子节点个数、二叉树高度

    #pragma once
    #include<stdio.h>
    #include<stdlib.h>
    #include<assert.h>
    #include<stdbool.h>
    
    typedef int DataType;
    typedef struct Node{
        DataType val;
        struct Node* left;
        struct Node* right;
    };
    
    typedef struct BinaryTree{
    
        Node* root;
    
    }BinaryTree;
    
    //初始化
    void InitBinaryTree(BinaryTree * root)
    {
        root == NULL;
    }
    
    //前序遍历
    void preOrderTraversal(Node* root)
    {
        if (NULL == root)
            return;
        printf("%d  ", root->val);
        preOrderTraversal(root->left);
        preOrderTraversal(root->right);
    
    }
    
    //中序遍历
    void inOrderTraversal(Node* root)
    {
        if (NULL == root)
            return;
        inOrderTraversal(root->left);
        printf("%d  ", root->val);
        inOrderTraversal(root->right);
    
    }
    
    //后序遍历
    void postOrderTraversal(Node* root)
    {
        if (NULL == root)
            return;
        postOrderTraversal(root->left);
        postOrderTraversal(root->right);
        printf("%d  ", root->val);
    
    }
    
    //节点个数
    int count = 0;
    void getcount1(Node* root)
    {
        if (NULL == root)
            return;
        count++;
        getcount1(root->left);
        getcount1(root->right);
    }
    
    //节点个数
    int getcount2(Node* root)
    {
        if (NULL == root)
            return 0;
        
        int left = getcount2(root->left);
        int right = getcount2(root->right);
        return left + right + 1;
    }
    
    
    int myMax(int a, int b)
    {
        return (a > b ? a:b);
    }
    //二叉树高度
    int getHight(Node * root)
    {
        if (NULL == root)
            return 0;
        int left = getHight(root->left);
        int right = getHight(root->right);
        return myMax(left, right)+1;
    
    }
    
    //统计二叉树叶子节点
    int getLeafCount(Node* root)
    {
        if (NULL == root)
            return 0;
        else if (NULL == root->left  && NULL ==root->right)
            return 1;
        else
        {
            int left = getLeafCount(root->left);
            int right = getLeafCount(root->right);
            return left + right;
        }
    }
    
    //二叉树第k层节点个数
    int getKLeveCount(Node * root,int k)
    {
        assert(k >= 1);
        if (NULL == root)
            return 0;
        else if (1 == k)
            return 1; 
        else
        {
            int left = getKLeveCount(root->left, k - 1);
            int right = getKLeveCount(root->right, k - 1);
            return left + right;
            //return getKLeveCount(root->left, k - 1)+ getKLeveCount(root->right, k - 1);
        }
    }
    
    //在二叉树中查找值为V的节点
    Node* Find(Node* root,int v)
    {
        if (NULL == root)
            return NULL;
        if (v == root->val)
            return root;
        else
        {
            Node* FindLeft = Find(root->left,v);
            if (FindLeft != NULL){
                return FindLeft;
            }
            Node* FindRight = Find(root->right, v);
            if (FindRight != NULL){
                return FindRight;
            }
            else{
                return NULL;
            }
        }
    }
    住进火焰就成为萤火虫。
  • 相关阅读:
    oracle中xhost报错
    cronolog切割apache和tomcat日志
    rsync配置和同步数据
    Jenkins+GitHub+maven
    Git只获取部分目录的内容
    git命令综合
    tomcat(不仅仅是tomcat)通过熵池解决在linux启动应用慢
    iptables之ipset集群工具
    Python中yield表达式的使用
    对于python中出现UnicodeDecodeError问题的解决方案
  • 原文地址:https://www.cnblogs.com/fengkun/p/12028519.html
Copyright © 2011-2022 走看看