C++二叉树基本问题

1.非梯归遍历

先序遍历

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

	Node(int data) {
		this->value = data;
		left = right = NULL;
	}
};
	void preOrderUnRecur(Node* head)
	{
		if (head == NULL)
		{
			return;
		}
		cout << "saa" << endl;
		stack<Node* >* s = new stack<Node* >;
		s->push(head);
		while (!s->empty())
		{
			head = (s->top());
			cout << head->value << "  " << endl;
			s->pop();
			if (head->right != NULL)
			{
				s->push(head->right);
			}
			if (head->left != NULL)
			{
				s->push(head->left);
			}

	
		}
		delete s;
	}

中序遍历

	void inOrderUnRecur(Node* head)
	{
		if (head == NULL)
		{
			return;
		}
		stack<Node* >* s = new stack<Node* >;

		while (!s->empty()|| head!=NULL)
		{
			
			if (head != NULL)
			{
				s->push(head);
				head = head->left;
			}
			else
			{
				head = (s->top());
				cout << head->value << "  " << endl;
				s->pop();
				head = head->right;
						
			}
		}
		delete s;
	}

后序遍历

	void posOrderUnRecur(Node* head)
	{
		if (head == NULL)
		{
			return;
		}
		stack<Node*>s1;
		stack<Node*>s2;
		s1.push(head);
		Node* cur = NULL;
		while (!s1.empty())
		{
			cur = s1.top();
			s1.pop();
			s2.push(cur);
			if (cur->left != NULL)
			{
				s1.push(cur->left);
			}
			if (cur->right != NULL)
			{
				s1.push(cur->right);
			}
		}
		while (!s2.empty())
		{
			cout << s2.top()->value << endl;
			s2.pop();
		}

	}

二叉树的最大宽度

	int TreeMaxWidth(Node* head)
	{
		if (head == NULL)
		{
			return -1;
		}
		Node* left = NULL;
		Node* right = NULL;
		queue<Node* >q_tree;
		map<Node*, int>m_tree;
		m_tree.insert(make_pair(head,1));
		int level = 0;
		int count = 1;
		//vector<int>v;
		//v[0] = 1;
		int max_isn = INT_MIN;
		q_tree.push(head);
		while (!q_tree.empty())
		{
			head = q_tree.front();
			q_tree.pop();
			left = head->left;
			right = head->right;
			if (left != NULL)
			{
				m_tree.insert(make_pair(left,m_tree[head]+1));
				q_tree.push(left);
			}
			if(right!=NULL)
			{
				m_tree.insert(make_pair(right, m_tree[head] + 1));
				q_tree.push(right);

			}
			if (m_tree[head] > level)
			{

				level = m_tree[head];
			
				count = 1;
			}
			else
			{
				count++;
			//	v[level - 1] = count;
				
			}
			cout << "    count" << count;
			max_isn = max(count, max_isn);
		}
		cout << endl;
		return max_isn;
	}

二叉树是否搜索二叉树(左边的值大于右边值)

bool isBST(Node* head) 
	{
		if (head == NULL)
		{
			return true;
		}
		list<Node*>s;
		int m_value = INT_MIN;
		inprocess(head,&s);
		list<Node*>::iterator p1;
		for (p1 = s.begin();p1 != s.end();p1++)
		{
			if ((*p1)->value > m_value)
			{
				m_value = (*p1)->value;
			}
			else 
			{
				return false;
			}
		}
		return true;
	}
	void inprocess(Node* head, list<Node*> *s) 
	{
		if (head == NULL)
		{
			return;
		}
		inprocess(head->left,s);
		s->push_back(head);
		inprocess(head->right,s);
	}

二叉树是否为完全二叉树

	bool isCBT(Node* head)
	{
		if (head == NULL)
		{
			return true;
		}
		queue<Node*>q;
		q.push(head);

		Node* cur = NULL;
		Node* l = NULL;
		Node* r = NULL;
		bool leaf = false;
		while (!q.empty())
		{
			cur = q.front();
			l = cur->left;
			r=cur->right;
			q.pop();
			if ((l == NULL && r != NULL) || (leaf&&(r!=NULL||l!=NULL)) )
			{
				return false;
			}
			if (l != NULL)
			{
				q.push(l);
			}
			if (r != NULL)
			{
				q.push(r);
			}
			else
			{
				leaf = true;
			}
		}
		return true;
	}

二叉树是否为平衡二叉树

class ReturnType
{
public:
	int  height;
	bool isBalaned;
	ReturnType(int h,bool isB)
	{
		this->height = h;
		this->isBalaned = isB;
	}
};
class IsBalancedTree
{
public:
	
	bool IsBalan(Node* head)
	{
		if (head == NULL)
		{
			return true;
		}
		return process1(head).isBalaned;
	}
	ReturnType process1(Node *x)
	{
		if (x == NULL)
		{
			return ReturnType(0,true);
		}
		ReturnType l = process1(x->left);
		ReturnType r = process1(x->right);
		int height = max(l.height, r.height)+1;

		bool flag = (r.isBalaned) && (l.isBalaned) && abs(l.height - r.height)<2;
		return ReturnType(height,flag);
	}
};

最低共同祖先

class LowestCommonAncestor
{
public:

	Node* lca(Node* head, Node* node1, Node* node2) {
		if (head == NULL || head == node1 || head == node2) {
			return head;
		}

		Node* left = lca(head->left, node1, node2);
		Node* right = lca(head->right, node1, node2);
		if (left != NULL && right != NULL) {
			return head;
		}
		return left != NULL ? left : right;

	}
	Node* lowestAncestor(Node* head, Node* node1, Node* node2)
	{
		if (head == NULL || head == node1 || head == node2)
		{
			return NULL;
		}
		map<Node*, Node*>h_m;
		h_m.insert(make_pair(head,head));
		process(head,&h_m);
		set<Node*>set_n;
		Node* cur = node1;

		while (cur!= h_m[cur])
		{
			cout << "caing"<<endl;
			

			set_n.insert(cur);
			cur = h_m[cur];
		}
		set_n.insert(head);
		cur = node2;

		while (cur != h_m[cur])
		{
			
			if (set_n.count(cur)==1)
			{
				
				
					
					return cur;			
				
			}


			cur = h_m[cur];

		}




		return cur;
	

	}

	void process(Node *head, map<Node*, Node*>*m)
	{
		if (head==NULL)
		{
			return;
		}
		m->insert(make_pair(head->left, head));
		m->insert(make_pair(head->right, head));
		process(head->left,m);
		process(head->right, m);

		
		
	}
	
};

后继节点

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

	Node(int data) {
		this->value = data;
		left = NULL;
		right = NULL;
	}
};
class Code08_SuccessorNode
{
public:
	Node* getSuccessorNode(Node* node)
	{
		if (node->right != NULL)
		{
			return rightChild(node->right);
		}
		else
		{
			Node* parent = node->parent;
			while (parent->left != node && parent != NULL)
			{
				node = parent;
				parent = node->parent;
			}
			return parent;
		}
	}
	Node* rightChild(Node* node)
	{
		if (node == NULL)
		{
			return NULL;
		}
		while (node->left != NULL)
		{
			node = node->left;

		}
		return node;
	}

};

序列化和反序列二叉树

class SerializeAndReconstructTree
{
public:
	void string_split(const string& str, const char split, queue<string>& res_string)
	{
		string strs = str;
		int pos = strs.find(split);
		while (pos != strs.npos)
		{
			string temp = strs.substr(0, pos);
			res_string.push(temp);
			strs = strs.substr(pos + 1, strs.size());
			pos = strs.find(split);
		}
	}
	void serialByPre(Node *head,string &origin)
	{
		if (head == NULL)
		{
			origin += "#!";
			return ;
		}
		origin += to_string(head->value);
		origin += "!";
		serialByPre(head->left,origin);

		serialByPre(head->right, origin);
		
	}
	Node* reconByPreString(queue<string>& res_string)
	{
		string str=res_string.front();
		res_string.pop();
		if (str._Equal( "#"))
		{
			return NULL;
		}

		int num = stoi(str.c_str());
		Node* head = new Node(num);
		head->left = reconByPreString(res_string);
		head->right = reconByPreString(res_string);
		return head;

	}
	string width_string(Node *head)
	{
		queue<Node*>q;
		q.push(head);
		Node* cur = head;
		string str;
		str = to_string(cur->value);
		
		str += "!";
		cout << "str:" << str << endl;
		while (!q.empty())
		{
			cur=q.front();

			q.pop();
			if (cur->left != NULL)
			{
				str += to_string(cur->left->value);
				str += "!";
				q.push(cur->left);
			}
			else
			{
				str += "#!";
			}
			if (cur->right != NULL)
			{
				str += to_string(cur->right->value);
				str += "!";
				q.push(cur->right);
			}
			else
			{
				str += "#!";
			}
					
		}
		return str;
	}
	Node* width_Node_dud(string str)
	{
		if (str == "#!")
		{
			return NULL;
		}
		queue<string>q;
		this->string_split(str,'!',q);
		string temp_str = q.front();
		q.pop();
		int num = stoi(temp_str.c_str());
		Node* head = new Node(num);
		queue<Node*>q_N;
		q_N.push(head);
		Node* cur = NULL;
		while (!q_N.empty()&&!q.empty())
		{
			cur = q_N.front();
			
			q_N.pop();



			temp_str= q.front();
			q.pop();
			if (temp_str._Equal("#"))
			{
				cur->left = NULL;
			}
			else
			{
				num = stoi(temp_str.c_str());
				cur->left = new Node(num);
				q_N.push(cur->left);
			}



			temp_str = q.front();
			q.pop();
			if (temp_str._Equal("#"))
			{
				cur->right = NULL;
			}
			else
			{
				num = stoi(temp_str.c_str());
				cur->right = new Node(num);
				q_N.push(cur->right);
			}
		}
		return head;
	}


};

折纸问题

class PaperFolding {
public:
	void printAllFolds(int n)
	{
		printProcess(1,n,true);

	}
	void printProcess(int i,int n,bool down)
	{
		if (i > n)
		{
			return;
		}
		printProcess(i+1,n,true);
		if (down)
		{
			cout << "down" << endl;
		}
		else cout << "up" << endl;
		printProcess(i + 1, n, false);
	}
};

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值