• 二叉树的部分操作(递归)


    二叉树的部分操作。前序、中序、后序遍历,节点个数、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;
            }
        }
    }
    住进火焰就成为萤火虫。
  • 相关阅读:
    在集群环境中使用 EhCache 缓存系统|RMI 集群模式
    oracle中的针对该库的表
    oracle:触发器,自治事务 trigger
    PL/SQL中查询某的时间段内所有执行的sql
    金额input框控制只能小数点后有两位的有效数字
    JS判断键盘上的上下左右键
    MySQL按照汉字的拼音排序
    JAVA经典总结
    普通for循环遍历LinkedList弊端
    EASYUI 1.4版 combobox firefox 下不支持中文检索的问题
  • 原文地址:https://www.cnblogs.com/fengkun/p/12028519.html
Copyright © 2020-2023  润新知