LeetCode 655. Print Binary Tree

Print a binary tree in an m*n 2D string array following these rules:

The row number m should be equal to the height of the given binary tree.
The column number n should always be an odd number.
The root node’s value (in string format) should be put in the exactly middle of the first row it can be put. The column and the row where the root node belongs will separate the rest space into two parts (left-bottom part and right-bottom part). You should print the left subtree in the left-bottom part and print the right subtree in the right-bottom part. The left-bottom part and the right-bottom part should have the same size. Even if one subtree is none while the other is not, you don’t need to print anything for the none subtree but still need to leave the space as large as that for the other subtree. However, if two subtrees are none, then you don’t need to leave space for both of them.
Each unused space should contain an empty string “”.
Print the subtrees following the same rules.
Example 1:
Input:
1
/
2
Output:
[[“”, “1”, “”],
[“2”, “”, “”]]
Example 2:
Input:
1
/ \
2 3
\
4
Output:
[[“”, “”, “”, “1”, “”, “”, “”],
[“”, “2”, “”, “”, “”, “3”, “”],
[“”, “”, “4”, “”, “”, “”, “”]]
Example 3:
Input:
1
/ \
2 5
/
3
/
4
Output:

[[“”, “”, “”, “”, “”, “”, “”, “1”, “”, “”, “”, “”, “”, “”, “”]
[“”, “”, “”, “2”, “”, “”, “”, “”, “”, “”, “”, “5”, “”, “”, “”]
[“”, “3”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”]
[“4”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”]]
Note: The height of binary tree is in the range of [1, 10].

题目大意:按照以下规则在一个m * n的二维字符串数组中打印一个二叉树:
行号m应该等于给定二叉树的高度。
列号n应始终为奇数。
根节点的值(以字符串格式)应该放在第一行的正中间。 根节点所属的行和列将剩余空间分成两部分(左下部分和右下部分)。 您应该在左下角打印左侧子树,并在右下侧打印右侧子树。 左下部分和右下部分应该具有相同的尺寸。 即使一个子树不存在,而另一个子树不存在,您也不需要为无子树打印任何东西,但仍然需要留下与另一子树相同的空间。 但是,如果两个子树都没有,那么你就不需要为它们留下空间。
每个未使用的空间应该包含一个空字符串“”。
按照相同的规则打印子树。

分析:首先计算出树的高度h,然后建立h行、(2的h次方-1)列的字符串数组,进行先序遍历,设立l和r表示当前填充的子树对应的字符串数组的左右下标区域,h表示当前的层数,每次将root->val的值存储入ans[h][mid]中,其中mid=(l+r)/2,然后继续遍历左子树和右子树,将区间分为0~mid-1和mid+1~r,高度h加1,直至遍历完成后返回ans数组~

 

LeetCode 537. Complex Number Multiplication

Given two strings representing two complex numbers.

You need to return a string representing their multiplication. Note i2 = -1 according to the definition.

Example 1:
Input: “1+1i”, “1+1i”
Output: “0+2i”
Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.
Example 2:
Input: “1+-1i”, “1+-1i”
Output: “0+-2i”
Explanation: (1 – i) * (1 – i) = 1 + i2 – 2 * i = -2i, and you need convert it to the form of 0+-2i.
Note:

The input strings will not have extra blank.
The input strings will be given in the form of a+bi, where the integer a and b will both belong to the range of [-100, 100]. And the output should be also in this form.

题目大意:给两个复数,求两个数的乘积~

分析:使用sscanf和ssprinf,(m+ni)*(p+qi)=(m*p-n*q)*(n*p+m*q) i

 

LeetCode 654. Maximum Binary Tree

Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:

The root is the maximum number in the array.
The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.
Construct the maximum tree by the given array and output the root node of this tree.

Example 1:
Input: [3,2,1,6,0,5]
Output: return the tree root node representing the following tree:

6
/ \
3 5
\ /
2 0
\
1
Note:
The size of the given array will be in the range [1,1000].

分析:用递归,l和r分别表示使用在nums[l]到nums[r]区间内的数字进行建树,每次通过idx找到l到r之间的最大值对应的下标idx,使用nums[idx]值new一个新TreeNode结点root,然后将root的左边用l~idx-1建树,右边用idx+1~r建树,然后返回root结点即完成了建树~

 

pair的用法 make_pair

pair<string, int> p1(“123”, 99), p2, p3;

p2.first = “abc”, p2.second = 2;

p3 = make_pair(“dce”, 1);

cout << p1.first << ” “ << p1.second;

pair<string, int> 相当于一个类型名称,如果要创建一个这个类型的数组,可以写vector<pair<string, int>>

bitset 位运算

#include <bitset>

0101101010    //一个bitset类型的变量
9876543210  //对应元素的下标

初始化
bitset在定义时,要明确bitset含有多少位,不满左边会自动补0
bitset<5> bit1(“11101”); //最简单的初始化
bitset<n> b; //b有n位,每位都为0
bitset<n> b(u); //b是unsigned long型u的一个副本
bitset<n> b(s); //b是string对象s中含有的位串的副本

查找
bit1.size()//返回二进制总位数
bit1.count()  // 返回bit1里1的个数
bool flag = bit1.any(); // 等价于bit1.count() != 0时返回true
bool flag = bit1.none(); // 等价于bit1.count() == 0时返回true
bool flag = bit1.test(5); // bit[5]等于1的时候返回true,表示这一位被设置了数

操作
bit1.set()//所有位设为1
bit1.set(pos)//下标为pos的位设为1
bit1.reset//所有位设为0
bit1.reset(pos)//下标为pos的位设为0
bit1.flip()//所有位取反
bit1.flip(pos)//pos位取反
bit1.to_ulong()//返回一个unsigned long long 整数

binary_search()、upper_bound()、lower_bound() 二分查找

vector<int> a = {0,1,2,2,3,4}; 使用前提是a已经是升序排列

cout << binary_search(a.begin(), a.end(), 3); // 找是否存在3,return false or true

auto it = upper_bound(a.begin(), a.end(), 2);// 从左到右返回第一个大于2的数字的地址
auto itt = lower_bound(a.begin(), a.end(), 2);// 从左到右返回第一个大于等于2的数字的地址
// 找不到就返回a.end()