CCI_chapter 4 trees and Grapths
4.1Implement a function to check if a tree is balanced For the purposes of this question,a balanced tree is defned to be a tree such that no two leaf nodes difer in distance from the root by more than one
http://www.cnblogs.com/graph/archive/2013/04/12/3016433.html
4.2DFS
4.3Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height
http://www.cnblogs.com/graph/p/3184984.html
4.4 Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (eg, if you have a tree with depth D, you’ll have D linked lists)
http://www.cnblogs.com/graph/p/3251831.html
题目类似,就不多做了
4.5Write an algorithm to fnd the ‘next’ node (e g , in-order successor) of a given node in a binary search tree where each node has a link to its parent
struct Node
{
int data;
struct Node* left;
struct Node* right;
struct Node* parent;
};
Node * minNode(Node * cur){ while(NULL != cur->left){
cur = cur->left;
}
return cur; }
Node * inOrderSucc(Node *cur){
if(cur == NULL) return NULL;
if(NULL != cur->right)
return minNode(cur->right); Node * p = cur->parent;
while(NULL != p && p->right == cur){
cur = p;
p = p->parent;
} return p; }
reference : http://www.geeksforgeeks.org/inorder-successor-in-binary-search-tree/(不喜欢CCI上的渣渣答案)
4.6Design an algorithm and write code to fnd the frst common ancestor of two nodes in a binary tree Avoid storing additional nodes in a data structure NOTE: This is not
necessarily a binary search tree
http://www.cnblogs.com/graph/p/3271292.html
4.7 You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes Create an algorithm to decide if T2 is a subtree of T1
无聊的一道题,解法完全没有体现出来大数据
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
bool isMatch(TreeNode * T1, TreeNode &T2);
bool isSubtree(TreeNode *T1, TreeNode *T2){ if(T2 == NULL) return true;
if(NULL == T1) return false;
if(T1->val == T2->val && ismatch(T1, T2)){
return true;
}
return isSubtree(T1->left , T2) || isSubtree(T1->right, T2) ; }
bool isMatch(TreeNode * T1, TreeNode &T2){
if(T1 == NULL && T2 == NULL) return true;
if(T1 == NULL || T2 == NULL) return false; if(T1->val != T2->val) return false; return isMatch(T1->left , T2->right) && isMatch(T1->right, T2->right); }
4.8 You are given a binary tree in which each node contains a value Design an algorithm to print all paths which sum up to that value Note that it can be any path in the tree
- it does not have to start at the root
CCI 给的答案实在不爽,明明就是一个后续遍历的应用。非要搞的莫名其妙的。
声明: 以下代码只是写出了我的思路,没有经过测试
struct Node{
int val;
Node * left;
Node * right;
Node(int value):val(value), left(NULL),right(NULL){}
};
void print(stack<int> s){
while(!s.empty()){
cout<<s.top() <<" ";
s.pop();
}
cout<<endl;
}
void findSum(Node *root,int sum, vector<stack<int>> &path, vector<stack <int>> &path_sum ){
if(root == NULL) return ;
if(root->left == NULL && root->right == NULL){
stack<int> p,s;
p.push(root->val);
s.push(root->val);
path.push_back(p);
path_sum.push_back(s);
return;
}
vector<stack<int>> pathLeft, pathRight , sumLeft, sumRight;
if(root->left != NULL)
findSum(root->left, sum, pathLeft,sumLeft);
if(root->right != NULL)
findSum(root->right, sum, pathRight, sumRight);
int cur = root->val;
for(int i = ; i < pathLeft.size(); i++)
{
pathLeft[i].push(cur);
int top = sumLeft[i].top() + cur;
sumLeft[i].push(top);
if(top == sum)
print(pathLeft[i]);
path.push_back(pathLeft[i]);
path_sum.push_back(sumLeft[i]);
}
for(int i = ; i< pathRight.size(); i++)
{
pathRight[i].push_back(cur);
int top = sumRight[i].top() + cur;
sumRight[i].push(top);
if(top == sum)
print(pathRight[i]);
path.push_back(pathRight[i]);
path_sum.push_back(sumRight[i]);
}
}
CCI_chapter 4 trees and Grapths的更多相关文章
- [C#] C# 知识回顾 - 表达式树 Expression Trees
C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...
- hdu2848 Visible Trees (容斥原理)
题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...
- [LeetCode] Minimum Height Trees 最小高度树
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 2 Unique Binary Search Trees II_Leetcode
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- Finger Trees: A Simple General-purpose Data Structure
http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...
- Christmas Trees, Promises和Event Emitters
今天有同事问我下面这段代码是什么意思: var MyClass = function() { events.EventEmitter.call(this); // 这行是什么意思? }; util.i ...
- 【leetcode】Unique Binary Search Trees (#96)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- Android 5.0 之SwipeRefreshLayout
金田 下拉刷新是一种比较常用的效果,Android 5.0之前官方并未提供类似的控件,App中主要是用的第三方库,例如PullToRefresh,ActionBar-PullToRefresh等.刚好 ...
- JQuery 插件 - 弹窗:BlockUI
JQuery的弹窗插件,网上实在有很多做的比较好的,比如artDialog.layer,甚至EasyUI等等.这些在效果上做的非常好.但我觉得一个小小的弹窗提示,没有必要引用这些(其实是有点大材小用了 ...
- iOS 后台定位被拒注意事项
iOS 后台定位被拒的原因很简单就是没有达到苹果对后台定位的要求. 本地要求: 1.在plist文件中添加字段 "Privacy - Location Always Usage Descri ...
- Java_io体系之PipedWriter、PipedReader简介、走进源码及示例——14
Java_io体系之PipedWriter.PipedReader简介.走进源码及示例——14 ——管道字符输出流.必须建立在管道输入流之上.所以先介绍管道字符输出流.可以先看示例或者总结.总结写的有 ...
- String 和 InputStream 互转方式
/** * 利用BufferedReader实现Inputstream转换成String <功能详细描述> * * @param in * @return String */ public ...
- 数据库VIEW(视图)
视图是基于 SQL 语句的结果集的可视化的表. 视图包括行和列,就像一个真实的表.视图中的字段就是来自一个或多个数据库中的真实的表中的字段. 我们能够向视图加入 SQL 函数.WHERE 以及 JOI ...
- Linux进程实时监控 - htop
htop 是一个 Linux 下的交互式的进程浏览器,top的增强版 htop: 进入:htop 退出:按q键 常用操作: ...
- ZOJ 3511 不相交切切多边形 线段树求最大边数
题意: n多凸边形 m刀 (把n切m刀,问切完后的图形中 最多的边数 是多少) 切a点-b点 数据保证切的刀不会相交 思路: 2点之间的剩余点数就是边数, 把a-b距离 近 排序 切完一刀就统计一下切 ...
- sun.misc.BASE64Encoder是内部专用 API, 可能会在未来发行版中删除
简介 MEVAN打包遇到问题“sun.misc.BASE64Encoder是内部专用 API, 可能会在未来发行版中删除”,属于警告!项目虽然能正常运行,但是有警告就是一种隐患,要将隐患消灭在萌芽中. ...
- Cycling Label
Cycling Label 来源: github/BBCyclingLabel Licence: Apache 2.0 作者: Bruno de Carvalho 分类: 标签(Label) 平台: ...