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. FICO基础知识(二)

    FI中的maser data: COA (Chart Of Account)  科目表 Account 科目 Vendor master dada  供应商主数据 Customer master da ...

  2. 如何利用Hadoop存储小文件

    **************************************************************************************************** ...

  3. js跨域请求jsonp解决方案-最简单的小demo

    这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...

  4. ceph API之PHP的客户端连接

    下载v2的SDK开发包http://pear.amazonwebservices.com/get/sdk-latest.zip 解压到目录下: unzip sdk-latest.zip &&a ...

  5. BZOJ3526[Poi2014]Card——线段树合并

    题目描述 有n张卡片在桌上一字排开,每张卡片上有两个数,第i张卡片上,正面的数为a[i],反面的数为b[i].现在,有m个熊孩子来破坏你的卡片了!第i个熊孩子会交换c[i]和d[i]两个位置上的卡片. ...

  6. BZOJ3172[Tjoi2013]单词——AC自动机(fail树)

    题目描述 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次. 输入 第一个一个整数N,表示有多少个单词,接下来N行每行一个单词.每个 ...

  7. HDU-2087-KMP-水题

    纯KMP #include <cstdio> #include <algorithm> #include <cstring> #include <ctype. ...

  8. ZOJ1363 Chocolate 【生成函数】 【泰勒展开】

    题目大意: 有c种不同的巧克力,每种无限个,意味着取出每种的几率每次为1/c.现在你需要取n次.然后将统计每种取出来的巧克力的数量.若为偶数则舍去,否则留下一个.问最后留下m个的概率是多少. 题目分析 ...

  9. 洛谷P3959 宝藏(NOIP2017)(状压DP,子集DP)

    洛谷题目传送门 Dalao的题解多数是什么模拟退火.DFS剪枝.\(O(3^nn^2)\)的状压DP之类.蒟蒻尝试着把状压改进了一下使复杂度降到\(O(3^nn)\). 考虑到每条边的贡献跟它所在的层 ...

  10. Leetcode 414.Fizz Buzz By Python

    写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz": 3.如果 n 同时是3和 ...