f*e
10 楼
http://www.mitbbs.com/article/JobHunting/32251211_3.html
有些细节省略了。
【在 w****x 的大作中提到】
:
: 具体???
有些细节省略了。
【在 w****x 的大作中提到】
:
: 具体???
s*0
11 楼
store in array
【在 f*****e 的大作中提到】
: http://www.mitbbs.com/article/JobHunting/32251211_3.html
: 有些细节省略了。
【在 f*****e 的大作中提到】
: http://www.mitbbs.com/article/JobHunting/32251211_3.html
: 有些细节省略了。
e*s
16 楼
==
O(h^2)=O(logN^2) 怎么看得怪怪的
h ^ 2 不应该是等于 n 吗?
O(h^2)=O(logN^2) 怎么看得怪怪的
h ^ 2 不应该是等于 n 吗?
G*A
18 楼
update:修改了一下
/************/
这个行么?
int NumOfNodes(Node* p, int max_depth, int cur_depth, int index)
{
if (max_depth <= 1)
return max_depth;
if (!p)
return 0;
if (cur_depth == max_depth-1 && !p->left)
return 2 * index - 1;
if (cur_depth == max_depth-1 && !p->right)
return 2 * index;
int L = NumOfNodes(p->left, max_depth, cur_depth+1, index*2);
return L ? L : NumOfNodes(p->right, max_depth, cur_depth+1, index*2+1);
}
【在 w****x 的大作中提到】
: 完全二叉树求节点要怎么求来着?
: 有什么优化?
/************/
这个行么?
int NumOfNodes(Node* p, int max_depth, int cur_depth, int index)
{
if (max_depth <= 1)
return max_depth;
if (!p)
return 0;
if (cur_depth == max_depth-1 && !p->left)
return 2 * index - 1;
if (cur_depth == max_depth-1 && !p->right)
return 2 * index;
int L = NumOfNodes(p->left, max_depth, cur_depth+1, index*2);
return L ? L : NumOfNodes(p->right, max_depth, cur_depth+1, index*2+1);
}
【在 w****x 的大作中提到】
: 完全二叉树求节点要怎么求来着?
: 有什么优化?
e*s
19 楼
刚写的,思想是,node的总数等于left subtree + right subtree + 1;
如果left subtree perfect 直接算 2^h -1; 如果不,递归进去
同理right subtree
public static int nodesInCompleteBT(TreeNode root){
if(root == null)
return 0;
if(root.left == null) // one node tree
return 1;
int height = 1;
TreeNode node = root.left;
while(node != null)
{
node = node.left;
height++;
}
return nodesInCompleteBTHelper(root, height);
}
public static int nodesInCompleteBTHelper(TreeNode root, int h){
if(root == null)
return 0;
if(root.left == null)
return 1;
int L = 0, R = 0;
int count = 1;
TreeNode node = root.left;
while(node != null)
{
node = node.right;
count++;
}
if(count == h)
L = (int)Math.pow(2, h -1) - 1;
else
L = nodesInCompleteBTHelper(root.left, h - 1);
count = 1;
node = root.right;
while(node != null)
{
node = node.right;
count++;
}
if(count == h)
R = (int)Math.pow(2, h - 1) - 1;
else
R = nodesInCompleteBTHelper(root.right, h - 1);
return L + R + 1;
}
如果left subtree perfect 直接算 2^h -1; 如果不,递归进去
同理right subtree
public static int nodesInCompleteBT(TreeNode root){
if(root == null)
return 0;
if(root.left == null) // one node tree
return 1;
int height = 1;
TreeNode node = root.left;
while(node != null)
{
node = node.left;
height++;
}
return nodesInCompleteBTHelper(root, height);
}
public static int nodesInCompleteBTHelper(TreeNode root, int h){
if(root == null)
return 0;
if(root.left == null)
return 1;
int L = 0, R = 0;
int count = 1;
TreeNode node = root.left;
while(node != null)
{
node = node.right;
count++;
}
if(count == h)
L = (int)Math.pow(2, h -1) - 1;
else
L = nodesInCompleteBTHelper(root.left, h - 1);
count = 1;
node = root.right;
while(node != null)
{
node = node.right;
count++;
}
if(count == h)
R = (int)Math.pow(2, h - 1) - 1;
else
R = nodesInCompleteBTHelper(root.right, h - 1);
return L + R + 1;
}
p*2
20 楼
这个不错,面试很难想到呀。一会儿练练
【在 f*****e 的大作中提到】
: http://www.mitbbs.com/article/JobHunting/32251211_3.html
: 有些细节省略了。
p*2
21 楼
def getLeftHeight(root:Option[TreeNode]):Int={
var h=0
var node=root
while(!node.isEmpty){
h+=1
node=node.get.left
}
h
}
def getRightHeight(root:Option[TreeNode]):Int={
var h=0
var node=root
while(!node.isEmpty){
h+=1
node=node.get.right
}
h
}
def getNodesNum(h:Int):Int={
return math.pow(2,h).toInt-1
}
def totalNodes(root:Option[TreeNode]):Int={
var h=getLeftHeight(root)
var node=root
var total=0
while(h>0 && !node.isEmpty){
if (getRightHeight(node.get.left)==h-1){
total+=getNodesNum(h-1)+1
node=node.get.right
}
else{
total+=getNodesNum(h-2)+1
node=node.get.left
}
h-=1
}
total
}
var h=0
var node=root
while(!node.isEmpty){
h+=1
node=node.get.left
}
h
}
def getRightHeight(root:Option[TreeNode]):Int={
var h=0
var node=root
while(!node.isEmpty){
h+=1
node=node.get.right
}
h
}
def getNodesNum(h:Int):Int={
return math.pow(2,h).toInt-1
}
def totalNodes(root:Option[TreeNode]):Int={
var h=getLeftHeight(root)
var node=root
var total=0
while(h>0 && !node.isEmpty){
if (getRightHeight(node.get.left)==h-1){
total+=getNodesNum(h-1)+1
node=node.get.right
}
else{
total+=getNodesNum(h-2)+1
node=node.get.left
}
h-=1
}
total
}
w*x
30 楼
struct NODE
{
int nVal;
NODE* pLft;
NODE* pRgt;
NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL) {}
};
int getHeight(NODE* pNode, bool bLft)
{
int nRet = 0;
NODE* pIter = pNode;
while (NULL != pIter)
{
if (bLft)
pIter = pIter->pLft;
else
pIter = pIter->pRgt;
nRet++;
}
return nRet;
}
int getNum(int h)
{
return pow((double)2, h) -1;
}
int getNumberOfNodes(NODE* pRoot)
{
NODE* pNode = pRoot;
int h = getHeight(pNode, true);
int nRet = 0;
while (NULL != pNode)
{
int nTmp = getHeight(pNode->pLft, false);
if (nTmp == h - 1)
{
nRet += 1 + getNum(h-1);
pNode = pNode->pRgt;
}
else
{
nRet += 1 + getNum(h-2);
pNode = pNode->pLft;
}
h--;
}
return nRet;
}
{
int nVal;
NODE* pLft;
NODE* pRgt;
NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL) {}
};
int getHeight(NODE* pNode, bool bLft)
{
int nRet = 0;
NODE* pIter = pNode;
while (NULL != pIter)
{
if (bLft)
pIter = pIter->pLft;
else
pIter = pIter->pRgt;
nRet++;
}
return nRet;
}
int getNum(int h)
{
return pow((double)2, h) -1;
}
int getNumberOfNodes(NODE* pRoot)
{
NODE* pNode = pRoot;
int h = getHeight(pNode, true);
int nRet = 0;
while (NULL != pNode)
{
int nTmp = getHeight(pNode->pLft, false);
if (nTmp == h - 1)
{
nRet += 1 + getNum(h-1);
pNode = pNode->pRgt;
}
else
{
nRet += 1 + getNum(h-2);
pNode = pNode->pLft;
}
h--;
}
return nRet;
}
相关阅读
[合集] 告诉你!OPT 3个月没工作一点事都没有!问道G题(3)各位前辈,EPIC ON-SITE求经验。。同学们,找工作不要光看base, bonus也很重要G 家面试求祝福找几个headhunter比较好?你最大的优势是什么?提供 G 家内推OPT和H1B两问题咨询加州小公司面试问题这句话是表示至少要有绿卡吗?Authorized to work in the U.S on a permanent basis.求ASIC相关工作的referisilonH1b approval收到后,10/1生效前驾照更新问题on site临走时被告知还要见我一次,Google 这种公司会不会看人下菜碟?Dr. Dobb's 2012 Salary Surveyoracle offer有给signon bonus和rsu的么?刚才面了一A3女码,没来得及打架 (转载)One FEA position