Binary search (sorted) tree class of topics conventional thinking -Leetcode-thinking_record11

table of Contents

 

Binary search (sorted) tree defined

Binary search tree insertion node 

Binary search tree to find value

Binary search tree encoding and decoding

general idea

coding

decoding 

detail design

Coding part

Decoding section

Code

Reverse Number

general idea

detail design

Code


Binary search (sorted) tree defined

Binary search tree insertion node 

Binary search tree to find value

Binary search tree encoding and decoding

LeetCode 449.Serialize and Deserialize BST

Given a binary search tree, look for trees to achieve encoding and decoding functionality of the binary. The coding is about binary search tree into a string, the string to decode the upcoming binary search tree. Without limiting what encoding algorithm to use, just to ensure that when the call to binary search tree coding function may recall the decoding to recover.

general idea

coding

 

decoding 

detail design

Coding part

Decoding section

Code

#include <stdio.h>


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

#include <string>
#include <vector>

void BST_insert(TreeNode *node, TreeNode *insert_node){
	if (insert_node->val < node->val){
		if (node->left){
			BST_insert(node->left, insert_node);
		}
		else{
			node->left = insert_node;
		}
	}
	else{
		if (node->right){
			BST_insert(node->right, insert_node);
		}
		else{
			node->right = insert_node;
		}
	}
}

void change_int_to_string(int val, std::string &str_val){
	std::string tmp;
	while(val){
		tmp += val % 10 + '0';
		val = val / 10;
	}
	for (int i = tmp.length() - 1; i >= 0; i--){
		str_val += tmp[i];
	}
	str_val += '#';
}

void BST_preorder(TreeNode *node, std::string &data){
	if (!node){
		return;
	}
	std::string str_val;
	change_int_to_string(node->val, str_val);
	data += str_val;
	BST_preorder(node->left, data);
	BST_preorder(node->right, data);
}

class Codec {
public:
    std::string serialize(TreeNode* root) {
    	std::string data;
        BST_preorder(root, data);
        return data;
    }
    TreeNode *deserialize(std::string data) {
    	if (data.length() == 0){
	    	return NULL;
	    }
    	std::vector<TreeNode *> node_vec;
    	int val = 0;
    	for (int i = 0; i < data.length(); i++){
	    	if (data[i] == '#'){
	    		node_vec.push_back(new TreeNode(val));
	    		val = 0;
	    	}
	    	else{
	    		val = val * 10 + data[i] - '0';
	    	}
	    }
	    for (int i = 1; i < node_vec.size(); i++){
    		BST_insert(node_vec[0], node_vec[i]);
    	}
    	return node_vec[0];
    }
};

void preorder_print(TreeNode *node,int layer){
	if (!node){
		return;
	}
	for (int i = 0; i < layer; i++){
		printf("-----");
	}
	printf("[%d]\n", node->val);
	preorder_print(node->left, layer + 1);
	preorder_print(node->right, layer + 1);
}

int main(){
	TreeNode a(8);
	TreeNode b(3);
	TreeNode c(10);
	TreeNode d(1);
	TreeNode e(6);
	TreeNode f(15);	
	a.left = &b;
	a.right = &c;
	b.left = &d;
	b.right = &e;
	c.left = &f;	
	Codec solve;	
	std::string data = solve.serialize(&a);
	printf("%s\n", data.c_str());
	TreeNode *root = solve.deserialize(data);
	preorder_print(root, 0);	
	return 0;
}

Reverse Number

LeetCode 315.Count of Smaller Numbers After Self

Known array nums, novelty array count, count represents a number in the right and nums [i] than nums [i] smaller elements [i].

general idea

 

detail design

Code

#include <stdio.h>

#include <vector>
struct BSTNode {
	int val;
	int count;
	BSTNode *left;
	BSTNode *right;
	BSTNode(int x) : val(x), left(NULL), right(NULL), count(0) {}
};

void BST_insert(BSTNode *node, BSTNode *insert_node, int &count_small){
	if (insert_node->val <= node->val){
		node->count++;
		if (node->left){
			BST_insert(node->left, insert_node, count_small);
		}
		else{
			node->left = insert_node;
		}
	}
	else{
		count_small += node->count + 1;
		if (node->right){
			BST_insert(node->right, insert_node, count_small);
		}
		else{
			node->right = insert_node;
		}
	}
}

class Solution {
public:
    std::vector<int> countSmaller(std::vector<int>& nums) {
    	std::vector<int> result;
    	std::vector<BSTNode *> node_vec;
    	std::vector<int> count;
    	for (int i = nums.size() - 1; i >= 0; i--){
    		node_vec.push_back(new BSTNode(nums[i]));
	    }
	    count.push_back(0);
	    for (int i = 1; i < node_vec.size(); i++){
	    	int count_small = 0;
    		BST_insert(node_vec[0], node_vec[i], count_small);
    		count.push_back(count_small);
    	}
        for (int i = node_vec.size() - 1; i >= 0; i--){
        	delete node_vec[i];
        	result.push_back(count[i]);
        }
        return result;
    }
};

int main(){
	int test[] = {5, -7, 9, 1, 3, 5, -2, 1};
	std::vector<int> nums;
	for (int i = 0; i < 8; i++){
		nums.push_back(test[i]);
	}
	Solution solve;
	std::vector<int> result = solve.countSmaller(nums);
	for (int i = 0; i < result.size(); i++){
		printf("[%d]", result[i]);
	}
	printf("\n");
	return 0;
}

 

Published 271 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_17846375/article/details/104806200