1310. Right-Heavy Tree

#include "iostream"
#include "cstdio"
using namespace std;

struct Node{
	int val;
	Node *left, *right;
	Node(int v = 0){
		val = v;
		left = NULL;
		right = NULL;
	}
};

inline void preOrder(Node *head){ //一定要加上inline才能过,否则会time limit
	if (head != NULL){
		printf(" %d", head->val);
		preOrder(head->left);
		preOrder(head->right);
	}
}
inline  void inOrder(Node *head){
	if (head != NULL){
		inOrder(head->left);
		printf(" %d", head->val);
		inOrder(head->right);
	}
}

inline  void postOrder(Node *head){
	if (head != NULL){
		postOrder(head->left);
		postOrder(head->right);
		printf(" %d", head->val);
	}
}

inline  void buildTree(Node *&head, const int value){ //这里使用指针引用
	if (head == NULL){
		head = new Node(value);
	}
	else {
		if (head->val < value){
			buildTree(head->right ,value);
		}
		else
			buildTree(head->left, value);
	}
}

int main(){
	int n;
	bool ok = false;
	while (scanf("%d", &n) != EOF){
		if (ok)
			printf("\n");
		ok = true;
		Node *root = NULL;
		for (int i = 0; i < n; i++){
			int tmp;
			scanf("%d", &tmp);
			buildTree(root, tmp);
		}

		printf("Inorder:");
		inOrder(root);
		printf("\n");
		printf("Preorder:");
		preOrder(root);
		printf("\n");
		printf("Postorder:");
		postOrder(root);
		printf("\n");
	}
	return 0;
}

时间: 2024-08-05 06:40:57

1310. Right-Heavy Tree的相关文章

cogs 1310. [HAOI2006]聪明的猴子

1310. [HAOI2006]聪明的猴子 ★   输入文件:monkey.in   输出文件:monkey.out   简单对比时间限制:1 s   内存限制:128 MB [题目描述] 在一个热带雨林中生存着一群猴子,它们以树上的果子为生.昨天下了一场大雨,现在雨过天晴,但整个雨林的地表还是被大水淹没着, 猴子不会游泳,但跳跃能力比较强,它们仍然可以在露出水面的部分植物的树冠上来回穿梭,以找到喜欢吃的果实. 现在,在这个地区露出水面的有N棵树,假设每棵树本身的直径都很小,可以忽略不计.我们在

树的平衡 AVL Tree

本篇随笔主要从以下三个方面介绍树的平衡: 1):BST不平衡问题 2):BST 旋转 3):AVL Tree 一:BST不平衡问题的解析 之前有提过普通BST的一些一些缺点,例如BST的高度是介于lgN和N之间的,如果是N的的话,显然效率很低,不是我们需要的:但是在实际情况中,BST的高度h = N的情况却经常出现,例如下图所示.在BST中search,insert的running time都等于BST的高度h,我们肯定希望高度h越小越好,best case就是lgN.下图的example 2的

树及其衍生算法(Trees and tree algorithms)

1,二叉树(Binary tree) 二叉树:每一个节点最多两个子节点,如下图所示: 相关概念:节点Node,路径path,根节点root,边edge,子节点 children,父节点parent,兄弟节点sibling, 子树subtree,叶子节点leaf node, 度level,树高hight 节点Node: 路径path:从一个节点到拧一个节点间的边 根节点root, 边edge:节点间的连线 子节点 children, 父节点parent, 兄弟节点sibling, 子树subtre

CF1208H Red Blue Tree

CF1208H Red Blue Tree 原本应该放在这里但是这题过于毒瘤..单独开了篇blog 首先考虑如果 $ k $ 无限小,那么显然整个树都是蓝色的.随着 $ k $ 逐渐增大,每个点都会有且仅有一次变色,我们考虑维护这个变色的时间 $ t $ .如果每个点的变色时间都已经被算出来,那么我们可以轻易解决题目的查询操作和修改 $ k $ , 也就是说修改 $ k $ 本身就是个假操作..只需要考虑的是修改单点颜色. 修改单点颜色,看起来就很 $ ddp $ .树链剖分后,用$ f(x)

easyui js取消选中 Tree 指定节点

取消所有选中 var rootNodes = treeObject.tree('getRoots'); for ( var i = 0; i < rootNodes.length; i++) { var node = treeObject.tree('find', rootNodes[i].id); treeObject.tree('uncheck', node.target); }

Maximum Depth of Binary Tree

这道题为简单题 题目: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 思路: 我是用递归做的,当然也可以用深搜和广搜,递归的话就是比较左右子树的深度然后返回 代码: 1 # Definition for a binary tre

538. Convert BST to Greater Tree 二叉搜索树转换为更大树

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi

SPOJ375 Query on a tree

https://vjudge.net/problem/SPOJ-QTREE 题意: 一棵树,每条边有个权值 两种操作 一个修改每条边权值 一个询问两点之间这一条链的最大边权 点数<=10000 多组测试数据,case<=20 Example Input: 1 3 1 2 1 2 3 2 QUERY 1 2 CHANGE 1 3 QUERY 1 2 DONE Output: 1 3 #include<cstdio> #include<iostream> #include&

POJ 1741 Tree(树的点分治,入门题)

Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21357   Accepted: 7006 Description Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v)=The min distance between node u and v.Give an in