leetcode --binary tree
1. 求深度:
recursive 遍历左右子树,递归跳出时每次加一。
int maxDepth(node * root)
{
if(roor==NULL)
return 0;
int leftdepth=maxDepth(root->leftchild);
int rightdepth=maxDepth(root->rightchild);
if(leftdepth>=rightdepth)
return leftdepth+1;
else
return rightdepth+1;
}
2. 广度搜索
使用队列
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> result;
if(!root)
return result;
int i=0;
queue<TreeNode *> q;
q.push(root);
while(!q.empty())
{
int c=q.size();
vector <int> t;
for(int i=0;i<c;i++)
{
TreeNode *temp;
temp=q.front();
q.pop();
t.push_back(temp->val);
if(temp->left)
q.push(temp->left);
if(temp->right)
q.push(temp->right);
}
result.push_back(t);
}
reverse(result.begin(),result.end());
return result;
}
};
3. 二叉查找树
由于二叉查找树是递归定义的,插入结点的过程是:若原二叉查找树为空,则直接插入;否则,若关键字 k 小于根结点关键字,则插入到左子树中,若关键字 k 大于根结点关键字,则插入到右子树中。
/**
* 插入:将关键字k插入到二叉查找树
*/
int BST_Insert(BSTree &T, int k, Node* parent=NULL)
{
if(T == NULL)
{
T = (BSTree)malloc(sizeof(Node));
T->key = k;
T->left = NULL;
T->right = NULL;
T->parent = parent;
return 1; // 返回1表示成功
}
else if(k == T->key)
return 0; // 树中存在相同关键字
else if(k < T->key)
return BST_Insert(T->left, k, T);
else
return BST_Insert(T->right, k, T);
}
构造二叉查找树:
/**
* 构造:用数组arr[]创建二叉查找树
*/
void Create_BST(BSTree &T, int arr[], int n)
{
T = NULL; // 初始时为空树
for(int i=0; i<n; ++i)
BST_Insert(T, arr[i]);
}
构造平衡二叉查找树:
每次找中间值插入:
class Solution {
public:
int insertTree(TreeNode * & tree, int a)
{
if(!tree)
{
// cout<<a<<endl;
tree=new TreeNode(a);
tree->left=NULL;
tree->right=NULL;
return 0;
}
if(a<tree->val)
{
return insertTree(tree->left,a);
}
else
return insertTree(tree->right,a);
}
int createBST(vector<int> &nums, int i,int j,TreeNode* &t)
{
if(i<=j&&j<nums.size())
{
int mid=i+(j-i)/2;
cout<<mid<<" "<<nums[mid]<<endl;
insertTree(t,nums[mid]);
createBST(nums,i,mid-1,t);
createBST(nums,mid+1,j,t);
}
return 0;
}
TreeNode* sortedArrayToBST(vector<int>& nums) {
if(nums.size()==0)
return NULL;
TreeNode *r=NULL;
createBST(nums,0,nums.size()-1,r);
/*
int i,j;
for(i=0, j=nums.size()-1;i<=mid-1 && j>=mid+1;)
{
cout<<i<<" "<<j<<endl;
insertTree(r,nums[i]);
i++;
insertTree(r,nums[j]);
j--;
}
if(i!=j)
{
if(i==mid-1)
{
cout<<nums[i]<<endl;
insertTree(r,nums[i]);
}
if(j==0)
{
cout<<nums[j]<<endl;
insertTree(r,nums[j]);
}
}
*/
return r;
}
};
不好,相当于每次从头遍历一次树, 没有必要。因为小的中间值直接插左边,大的中间值直接插右边
class Solution {
private:
TreeNode* helper(vector<int>& nums,int start,int end){
if(end<=start){
return NULL;
}
int mid = start + (end-start)/2;
TreeNode* root = new TreeNode(nums[mid]);
root->left = helper(nums,start,mid);
root->right = helper(nums,mid+1,end);
return root;
}
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
return helper(nums,0,nums.size());
}
};
leetcode --binary tree的更多相关文章
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- LeetCode: Binary Tree Traversal
LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Binary Tree Right Side View 二叉树的右侧视图
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- [LeetCode] Binary Tree Upside Down 二叉树的上下颠倒
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
随机推荐
- liunx上安装MySQL一个非常简单的方法
1.官网下载yum源 https://www.mysql.com/ 2.把yum源包上传到linux,安装. 执行命令安装 [root@bogon ~]# yum localinstall mysql ...
- 主成分分析PCA(Principal Component Analysis)在sklearn中的应用及部分源码分析
最近太忙,又有一段时间没写东西了. pca是机器学习中一个重要的降维技术,是特征提取的代表.关于pca的实现原理,在此不做过多赘述,相关参考书和各大神牛的博客都已经有各种各样的详细介绍. 如需学习相关 ...
- codeforces518B
Tanya and Postcard CodeForces - 518B 有个小女孩决定给他的爸爸寄明信片.她已经想好了一句话(即长度为n的字符串s),包括大写和小写英文字母.但是他不会写字,所以她决 ...
- BZOJ2124 等差子序列(树状数组+哈希)
容易想到一种暴力的做法:枚举中间的位置,设该位置权值为x,如果其两边存在权值关于x对称即合法. 问题是如何快速寻找这个东西是否存在.考虑仅将该位置左边出现的权值标1.那么若在值域上若关于x对称的两权值 ...
- MT【39】构造二次函数证明
这种构造二次函数的方法最早接触的应该是在证明柯西不等式时: 再举一例: 最后再举个反向不等式的例子: 评:此类题目的证明是如何想到的呢?他们都有一个明显的特征$AB\ge(\le)C^2$,此时构造二 ...
- BZOJ5312 冒险(势能线段树)
BZOJ题目传送门 表示蒟蒻并不能一眼看出来这是个势能线段树. 不过仔细想想也并非难以理解,感性理解一下,在一个区间里又与又或,那么本来不相同的位也会渐渐相同,线段树每个叶子节点最多修改\(\log ...
- 自学Linux Shell3.4-文件处理命令touch cp mv rm
点击返回 自学Linux命令行与Shell脚本之路 3.4-文件处理命令touch cp mv rm 1. touch命令 一是用于把已存在文件的时间标签更新为系统当前的时间(默认方式),它们的数据将 ...
- 【Luogu4719】动态dp
题面 洛谷 题解 等下发链接 代码: #include<iostream> #include<cstdio> #include<cstdlib> #include& ...
- Python面向对象编程和模块
在面向对象编程中,你编写表示现实世界中的事物和情景的类,并基于这些类来创建对象. 编写类时,你定义一大类对象都有的通用行为.基于类创建对象时,每个对象都自动具备这种通用行为,然后根据需要赋予每个对象独 ...
- A1019. General Palindromic Number
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...