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 ...
随机推荐
- vbox端口转发
端口转发:setting->network->adapter:attached to NAT.port forwarding rules->name protocol ...
- 【转】android ddms中查看线程释疑
原文网址:http://www.mobiletrain.org/lecture/doc/android/2011-05/457.html 大家都用过ddm,如果你用ddms查看一个程序的所有线程,你会 ...
- POJ-魔兽世界之一:备战
描述 魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部.两个司令部之间是依次排列的若干城市. 红司令部,City 1,City 2,……,City n,蓝司令部 两军的司令部都会制造武士.武士一共 ...
- (转)iOS keychain API及其封装
一. Keychain API KeyChain中item的结构为: 1.增加keychain Item OSStatus SecItemAdd (CFDictionaryRef attributes ...
- VirtualBox虚拟机网络设置
VirtualBox虚拟机网络设置 测试环境:物理机win10企业版本,VirtaulBox版本5.0.14,虚拟机安装Windows XP及linux系统 想实现虚拟机上网的最简单方式,修改虚拟机网 ...
- Find命令简介
Find命令主要用于目标的搜索,尽量做到少使用,因为find会消耗大量的系统资源. 使用该命令时,需要避开服务器运行高峰期,最好在指定的小范围内进行搜索,不要轻易使用全盘搜索. Find命令常用的参数 ...
- freemarker常用的基本命令
freemarker包括下面几个基本命令 if,else,elseif指令switch,case,default,break指令list,break指令include指令import 指令nopars ...
- 使用VS Code开发AngularJS 2 第一个应用程序
使用VS Code开发AngularJS 2 第一个应用程序 目录 运行环境 创建项目 安装依赖包 创建TypeScript应用程序 配置应用程序 运行应用程序 运行环境 运行环境: Windows ...
- ASP.NET常用编程代码(二)
1.绑定在DataList中的DropDownList private void dlistOrder_EditCommand(object source, System.Web.UI.WebCont ...
- RDLC 聚合函数按条件求和,显示"错误号"
=Sum(IIf(Fields!AValue.Value >0,Val(Fields!AValue.Value),)) 一直显示错误号 修改为 =Sum(IIf(Fields!AValue.Va ...