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. 数字证书签发,授权等相关以及https建立通信过程

    一直以来都对数字证书的签发,以及信任等事情一知半解.总算有个闲适的周末来总结和深入一下相关的知识. CA: CA(Certificate Authority)是证书的签发机构,它是负责管理和签发证书的 ...

  2. React Native & Web APP

    React Native Build native mobile apps using JavaScript and React https://facebook.github.io/react-na ...

  3. Leetcode 237.删除链表中的节点 By Python

    请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 -- head = [4,5,1,9],它可以表示为: 4 -> 5 -> 1 - ...

  4. BZOJ 4004 [JLOI2015]装备购买 | 线性基

    题目链接 Luogu P3265 题解 非常正常的线性基! 但是我不会线性基-- (吐槽:#define double long double 才过--) #include <cstdio> ...

  5. [luogu3505][bzoj2088][POI2010]TEL-Teleportation【分层图】

    题目大意 给出了一个图,然后让你加最多的边,让点\(1\)到\(2\)之间至少要经过5条边 解法 比较清楚,我们可以将这个图看作一个分层图,点\(1\)为第一层,再将\(2\)作为第五层,这样第一层和 ...

  6. 洛谷 P2341 [HAOI2006]受欢迎的牛 解题报告

    P2341 [HAOI2006]受欢迎的牛 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的"喜欢&q ...

  7. py3+requests+re+urllib,爬取并下载不得姐视频

    实现原理及思路请参考我的另外几篇爬虫实践博客 py3+urllib+bs4+反爬,20+行代码教你爬取豆瓣妹子图:http://www.cnblogs.com/UncleYong/p/6892688. ...

  8. cf1000D Yet Another Problem On a Subsequence (dp)

    设f[i]是以i为开头的好子序列的个数 那么有$f[i]=\sum\limits_{j=i+a[i]+1}^{N+1}{f[j]*C_{j-i-1}^{a[i]}}$(设f[N+1]=1)就是以i为开 ...

  9. hdu3506 Monkey Party (区间dp+四边形不等式优化)

    题意:给n堆石子,每次合并相邻两堆,花费是这两堆的石子个数之和(1和n相邻),求全部合并,最小总花费 若不要求相邻,可以贪心地合并最小的两堆.然而要求相邻就有反例 为了方便,我们可以把n个数再复制一遍 ...

  10. bug8 eclipse项目导入到myeclipse时 Target runtime com.genuitec.runtime.generic

    1.新导入的工程,出问题很大可能是jdk的版本问题导致,检查一下,发现jdk果然不一致,修改了jdk版本,但异常没有消除 2.网上查询下解决方案,原来在工程目录下的settings,有个文件也需要修改 ...