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 ...
随机推荐
- linux中断处理程序
Linux进行中断处理的4个步骤: 1.当中断产生,跳到统一入口IRQ_SVC 2.获取中断号 3.根据中断号找到irq_desc结构 4.从irq_desc结构中取出事先注册好的中断处理函数 Lin ...
- led驱动程序设计
LED的驱动程序很简单,按照张字符型设备驱动设计方法顺下来即可实现,这里主要讲几个注意事项. 一.在linux系统中,操作硬件不能够使用物理地址,一定要用虚拟地址.将物理地址转化为虚拟地址的函数如下: ...
- HDU_2055——刷题不要使用fflush()
Problem Description we define f(A) = 1, f(a) = -1, f(B) = 2, f(b) = -2, ... f(Z) = 26, f(z) = -26; G ...
- HDU5107---K-short Problem (线段树区间 合并、第k大)
题意:二维平面上 N 个高度为 Hi 建筑物,M次询问,每次询问输出 位于坐标(x ,y)左下角(也就是xi <= x && yi <= y)的建筑物中的第k高的建筑物的高 ...
- java与数据结构(6)---java实现链栈
栈之链式存储结构链栈 链栈 栈的链式存储结构成为链栈.链栈是没有头结点,头结点就是栈顶指针top. 代码结构 package list; public interface Stackable;公共接口 ...
- [饭后算法系列] "头尾移动" 排序列表
1. 问题 一个乱序列表(list), 只支持两种操作: 把一个元素移动到头部, 或者把一个元素移动到尾部. 需要设计一种算法, 使得移动次数最少而使列表有序 举两个例子: 1. {3,5,7,1,9 ...
- Servlet的生命周期?
Servlet何时被创建: 1,默认情况下,当WEB客户第一次请求访问某个Servlet的时候,WEB容器将创建这个Servlet的实例. 2,当web.xml文件中如果<servlet> ...
- 【KMP+DP】Count the string
KMP算法的综合练习 DP很久没写搞了半天才明白.本题结合Next[]的意义以及动态规划考察对KMP算法的掌握. Problem Description It is well known that A ...
- java.lang.NoSuchFieldError: INSTANCE
java.lang.NoSuchFieldError: INSTANCE异常,可能是包重复了. 我遇到的情况是maven里引入了一个JAR,而我又在lib里面引入了这个jar,并且版本还不相同,就出了 ...
- MySQL删除外键定义的方法
MySQL外键在定以后,如果我们不再需要这个外键,可以进行删除操作,下面就为您介绍MySQL删除外键定义的方法,供您参考. 不知道大家有没有发现,在定义外键的时候articles.member_id外 ...