zoukankan      html  css  js  c++  java
  • Cpp 二叉树

    #include<vector>
    #include<iostream>
    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<vector<int>> &result) {
        if(root==NULL) return;
        if(depth >= result.size()) result.push_back(vector<int>{});
        result[depth].push_back(root->val);
        LevelOrderTraverse(root->left,  depth + 1, result);
        LevelOrderTraverse(root->right, depth + 1, result);
    }
    void LevelOrderTraverse(BinaryTreeNode* root) {
        vector<vector<int>> result;
        LevelOrderTraverse(root, 0, result);
        for(auto line:result){
        	for(auto e:line) cout<<e<<' ';
        	cout<<endl;
    	} 
    }
    
    
    
    
    //二叉树节点总数目
    int NodeNum(struct BinaryTreeNode *root){
    	
    	if(root)
    		return 1+NodeNum(root->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<string> &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<string> binaryTreePaths(BinaryTreeNode* root) {
    	vector<string> result;
    	if(root!=NULL) binaryTreePaths(root,"",result);
    	return result;
    }
    
    
    
    int main(){
    	struct BinaryTreeNode *root;
    	
    	root = CreatBinaryTree();
    	
    	cout<<"二叉树总节点数为:"<<NodeNum(root)<<endl;
    	
    	cout<<"二叉树叶子节点数为:"<<LeafNum(root)<<endl;
    	
    	cout<<"二叉树最大深度(从根节点到最远叶子节点):"<<maxDepth(root)<<endl;
    	
    	cout<<"二叉树最小深度(从根节点到最近叶子节点):"<<minDepth(root)<<endl;
    	
    	cout<<"前序遍历结果:";
    	PreOrderTraverse(root);
    	cout<<endl;
    	
    	cout<<"中序遍历结果:";
    	InOrderTraverse(root);
    	cout<<endl;
    	
    	cout<<"后序遍历结果:";
    	LastOrderTraverse(root);
    	cout<<endl;
    	
    	cout<<"层次遍历结果:
    ";
    	LevelOrderTraverse(root);
    	cout<<endl;
    	
    	cout<<"二叉树所有路径:
    ";
    	vector<string> allPaths = binaryTreePaths(root);
    	for(auto e:allPaths) cout <<"	" << e <<endl;
    	
    	return 0;
    }
    /*
    测试用例:
    3 9 0 0 20 15 0 0 7 0 0
    
    3 9 15 0 0 7 0 0 20 0 0 
    
    1 2 0 5 0 0 3 0 0
    */
    
  • 相关阅读:
    UWP Composition API
    UWP VirtualizedVariableSizedGridView 支持可虚拟化可变大小Item的View(二)
    UWP VirtualizedVariableSizedGridView 支持可虚拟化可变大小Item的View(一)
    UWP 图片剪切旋转工具
    整理UWP中网络和设备信息获取的帮助类,需要的拿走。
    UWP 设备分辨率
    UWP webview 键盘bug,回退页面,键盘会弹一下。
    UWP 解决Webview在Pivot里面无法左右滑动的问题
    ngnix https
    nginx www解析失败问题解决
  • 原文地址:https://www.cnblogs.com/yejifeng/p/11308183.html
Copyright © 2011-2022 走看看