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的更多相关文章

  1. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  2. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  3. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  4. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  5. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  6. [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 ...

  7. [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 ...

  8. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  9. [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  10. [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. ...

随机推荐

  1. Log4J日志信息配置文件详解

    原文地址: http://blog.csdn.net/wuxintdrh/article/details/78282097 使用log4j 记录日志甚是方便,其提供了两种日志配置方式,log4j.pr ...

  2. Idea for Mac 过期 IntelliJ IDEA 2017 完美注册方法(附idea for Mac破解方法)

    Idea 不能使用了: 开始破解: (1)首先下载 jar包: https://download.csdn.net/download/engerla/10573069 放到位置: /Applicati ...

  3. centOS7 修改DNS

    #显示当前网络连接 #nmcli connection show NAME UUID TYPE DEVICE eno1 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 802 ...

  4. bzoj 2212 : [Poi2011]Tree Rotations (线段树合并)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2212 思路:用线段树合并求出交换左右儿子之前之后逆序对的数量,如果数量变小则交换. 实现 ...

  5. day11 reduce函数

    场景模拟: 序列元素在原有基础上加1 常规方法 简单但扩展性查 num1 = [1,2,3,4,5,6,7,8,9,100] res = 0 for i in num1: res += i print ...

  6. 【 Gym - 101138J 】Valentina and the Gift Tree(树链剖分)

    BUPT2017 wintertraining(15) 4 D Gym - 101138J 数据 题意 n个节点的一棵树,每个节点的权值为g,q个询问,树上的节点U-V,求U到V的路径的最大子段和. ...

  7. THUWC2018酱油记

    Day 0 今年的THUWC在我们学校,听说有pretest,感觉有不好的预感.... Day 1 早上7:00在校门口集合,车7:30以后才到,感觉就像在围观 期末考试.来到雅礼洋湖,在这里看到了初 ...

  8. Spark RDD操作之Map系算子

    在linux系统上安装solrCloud 1.依赖: JRE solr7.3 需要 java1.8 独立的zookeeper服务 ,zookeeper安装请参考: http://zookeeper.a ...

  9. 【BZOJ1064】【NOI2008】假面舞会(图论,搜索)

    题面 Description 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择一个自己喜欢的面 具.每个面具都有一个编号 ...

  10. luogu2680 [NOIp2015]运输计划 (tarjanLca+二分答案+树上差分)

    我们先不会就二分一下答案,设它是x,我们要判断它能不能满足 为了满足这个答案,我们就要让原本路径长度大于x的所有路径都经过某条边,而且这条边还要大于等于最长的路径-x 于是运用树上差分的思想,对于所有 ...