Fortune law left notes (continually updated)

1. stacks and queues

2. list problems

3. binary tree problem

3.1 to achieve binary sequence using non-recursive and recursive manner, and after the sequence preorder (C ++)

#include "pch.h"
#include <iostream>
#include <stack>
using namespace std;

class Node {
public:
	int value;
	Node *left;
	Node *right;

	Node(int data):value(data) {
	
	}
};


// 先序遍历 根 左 右
void preOrderRecur(Node *head) {
	if (head == nullptr)
		return;

	cout << head->value;
	
	preOrderRecur(head->left);
	preOrderRecur(head->right);
}

// 中序遍历 左 根 右
void inOrderRecur(Node *head) {
	if (head == nullptr)
		return;

	inOrderRecur(head->left);
	cout << head->value;
	inOrderRecur(head->right);
}

// 后序遍历 左 右 根
void posOrderRecur(Node *head)
{
	if (head == nullptr)
		return;

	posOrderRecur(head->left);
	posOrderRecur(head->right);
	cout << head->value;
   
}


void preOrderUnRecur(Node* head) {
	
	if (head != nullptr)
	{
		stack<Node*> stk;
		
		stk.push(head);
		while (!stk.empty())
		{
			head = stk.top();
			stk.pop();
			cout << head->value;

			if (head->right != nullptr)
				stk.push(head->right);
			if (head->left != nullptr)
				stk.push(head->left);
		}
	}
	
}


void inOrderUnRecur(Node* head) {

	if (head != nullptr)
	{
		stack<Node*>  stk;

		while (!stk.empty() || head != nullptr)
		{
			if (head != nullptr)
			{
				stk.push(head);
				head = head->left;
			}
			else
			{
				head = stk.top();
				stk.pop();
				cout << head->value;
				head = head->right;
			}
		}

	}
}


// 两个栈 s1 s2
void postOrderUnRecur(Node* head) {

	if (head != nullptr)
	{
		stack<Node*> stk1;
		stack<int> stk2;

		stk1.push(head);

		while (!stk1.empty())
		{
			head = stk1.top();
			stk1.pop();

			stk2.push(head->value);

			if (head->left != nullptr)
			{
				stk1.push(head->left);
			}
			if (head->right != nullptr)
			{
				stk1.push(head->right);
			}
		}

		while (!stk2.empty())
		{
			cout << stk2.top();
			stk2.pop();
		}

	}
	
}

// 用一个栈
void postOrderUnRecur(Node* head) {

	if (head != nullptr)
	{
		stack<Node* > stk;
		Node* h; // 表示最近弹出并打印的节点
		Node* c; // 表示 栈顶节点

		stk.push(head);
		h = head;
		c = nullptr;
		
		while (!stk.empty())
		{
			c = stk.top();
			if (h != c->left && h != c->right && c->left != nullptr)
			{
				stk.push(c->left);
			}
			else if (c->right != nullptr && h != c->right)
				stk.push(c->right);
			else
			{
				cout << stk.top()->value;
				stk.pop();
				h = c;
			}

		}
		
	}
}


3.2 print binary tree node boundary

Here Insert Picture Description
Here Insert Picture Description
[Answers]
[idea]

  1. First printed upside down the left-most node
  2. Preorder binary tree, printing each layer that does not belong to the leftmost or rightmost node, but they are also nodes leaf nodes.
  3. Print all layers total rightmost node from bottom to top, but most left node node can not be both, it is the most right node.
/*
按照标准打印二叉树的边界节点
*/

#include "pch.h"
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class Node {
public:
	int value;
	Node* left;
	Node* right;

	Node(int data) :value(data) {
	}
};

void printEdgeNode(Node* head) {

	if (head == nullptr)
		return;
	int height = getHeight(head, 0);
	/*Node** edgeMap  = new Node[height][2]();*/
	vector<vector<Node*> > edgeMap(height,vector<Node*>(2));
	setEdgeMap(head, 0, edgeMap);
	
		// 打印左边界
	for (int i = 0; i != edgeMap.size(); i++)
	{
		cout << edgeMap[i][0]->value;
	}
	
	// 打印既不是左边界又不是右边界的叶子节点
	printLeafNotInMap(head, 0, edgeMap);

	// 打印右边界,但不是左边界的节点
	for (int i = edgeMap.size() - 1; i != -1; i--)
	{
		if (edgeMap[i][0] != edgeMap[i][1])
		{
			cout << edgeMap[i][1]->value;
		}
	}

}

// 获取树的高度
int getHeight(Node* head,int l) {
	if (head == nullptr)
		return l;
	return max(getHeight(head->left, l + 1), getHeight(head->right, l + 1));

}

// 初始化edgemap
void setEdgeMap(Node* h, int l, vector<vector<Node*> > edgeMap)
{
	if (h = nullptr)
		return;
	
	edgeMap[l][0] = edgeMap[1][0] == nullptr ? h : edgeMap[l][0];
	edgeMap[l][1] = h;

	setEdgeMap(h->left, l + 1, edgeMap);
	setEdgeMap(h->right, l + 1, edgeMap);
}

// 打印既不是左边界,又不是右边界的叶子节点
void printLeafNotInMap(Node* h,int l, vector<vector<Node*> > edgeMap) {
	if (h = nullptr)
		return;

	if (h->left == nullptr && h->right == nullptr &&h != edgeMap[l][0] && h != edgeMap[l][1])
		cout << h->value;


	printLeafNotInMap(h->left, l + 1, edgeMap);
	printLeafNotInMap(h->right, l + 1, edgeMap);
}

The maximum distance between 3.3 binary tree node (Microsoft face questions)

Here Insert Picture Description

[Thinking]
maximum distance of three ways:

  1. The maximum distance h left subtree
  2. The maximum distance h right subtree
  3. left subtree h +1 h.left furthest distance (h) + h right sub-tree from the furthest distance h.right
#include <iostream>
#include <algorithm>
using namespace std;

class Node {
public:
	int value;
	Node* left;
	Node* right;

	Node(int data) :value(data) {
	}
};

int maxDistance(Node *head)
{
	int record = 0;
	return postOrder(head,record);

}

int postOrder(Node* head, int& record)
{
	if (head == nullptr) {
		record = 0;
		return 0;
	}

	int lmax = postOrder(head->left, record);
	int maxfromLeft = record;

	int rmax = postOrder(head->right, record);
	int maxfromRight = record;

	int curNodeMax = maxfromLeft + 1 + maxfromRight;
	record = max(maxfromLeft,maxfromRight)+1;

	return max(max(lmax, rmax), curNodeMax);

}

4. recursion and dynamic programming

5. String Problems

6. large data space limitations and

7. Bitwise

8. matrix arrays and problems

9. Other topics

1. The line of code greatest common divisor of two numbers

// 辗转相除法
int gcd(int m,int n) {
	return n == 0 ?  m : gcd(n, m%n);
}

2. the factorial of two issues

【topic】

Given a non-negative integer N, return to N! 0 Number of end results.

For example: 3! = 6 output 0
. 5! = 120 output is 1
1000000000! There is the end of 249 999 998 0, output is 249 999 998

Here Insert Picture Description

[Thinking] Topic:

  1. Count the number 5

[Thinking] Advanced Topic:

  1. Statistics Number 2

Guess you like

Origin blog.csdn.net/shaoye_csdn1/article/details/90601395