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 ...
随机推荐
- 部分无线终端不响应键盘事件(keydown,keypress,keyup)的解决办法
在无线侧实现搜索显示smartbox功能的时候,会对输入框绑定keydown.keyup.keypress事件,从而在检测到输入框的值发生改变时,发出请求拉取smartbox的内容. 但是,在iPho ...
- 【转】三星8552 手机提示升级系统 完成后重启 开机画面一直停留在三星的LOGO 一闪一闪 怎么办
原文网址:http://ask.zol.com.cn/q/309501.html 楼主你好,手机出现这种情况的话,可以先将电池取下来重新安装,如果不能的话,可以在关机状态下按住电源键+音量下键之后进入 ...
- HDOJ(HDU) 1555 How many days?(水题)
Problem Description 8600的手机每天消费1元,每消费K元就可以获赠1元,一开始8600有M元,问最多可以用多少天? Input 输入包括多个测试实例.每个测试实例包括2个整数M, ...
- [LeetCode] 28. Implement strStr() 解题思路
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- HDU-1241Oil Deposits
Description GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块.他们通过专业设备,来分析每个小块中是否 ...
- E - Redundant Paths - poj 3177(缩点求叶子节点)
题意:给一个图,想让每两个点之间都有两条路相连,不过特殊的是相同的两点之间多次相连被认为是一条边,现在求最少还需要添加几条边才能做到 分析:手欠没看清楚是相同的边只能相连一次,需要去重边,缩点后求出来 ...
- Spring容器的工具类
代码实现: package com.ht.util; import java.util.Map; import org.springframework.beans.BeansException; im ...
- CSS 入门
以下内容均来自 慕课网 CSS全称为"层叠样式表 (Cascading Style Sheets)",它主要是用于定义HTML内容在浏览器内的显示样式,如文字大小.颜色.字体加粗等 ...
- Zookeeper为什么总是奇数个
zookeeper有这样一个特性: [集群中只要有超过过半的机器是正常工作的,那么整个集群对外就是可用的] 也就是说如果有2个zookeeper,那么只要有1个死了zookeeper就不能用了,因为1 ...
- 每天进步一点点——Linux系统时间来处理
转载请注明出处:http://blog.csdn.net/cywosp/article/details/25839551 在程序中时间处理往往是一个麻烦的事.Linux系统提供了非常多关于时间处理的函 ...