CareerCup Chapter 4 Trees and Graphs】的更多相关文章

struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int val):val(val),left(NULL),right(NULL){} }; Not all binary trees are binary search trees. 4.1 Implement a function to check if a tree is balanced. For the purposes of this question…
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少会有一些问题,需要找一本数据结构的书恶补一下如何更加合理的设计节点. ? class TreeNode { public:     int treenum;       TreeNode** children;     int child_num;     int child_len;     in…
转载请注明出处:http://blog.csdn.net/ns_code/article/details/24744177     题目: Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height.     翻译: 给定一个有序数组(递增),敲代码构建一棵具有最小高度的二叉树.     思路: 要使二叉树的高度最小,则要尽量使其左右子树的节点数目相…
1.Type of Tree 1. Binary Tree: a binary tree is a tree in which each node has at most two child nodes(denoted as the left child and the right child). A directed edge refers to the link from the parent to the child (the arrows in the picture of the tr…
1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures? 字符串问题,需要先确定是不是只有ASCII码. 如果是,可以用char[256],也可以用位向量.位向量的实现参照<编程珠玑>.i&MASK就是取余.i>>SHIFT就是取商. class BitVector { pu…
二叉查换树,左孩子小于等于根,右孩子大于根. 完全二叉树,除最后一层外,每一层上的节点数均达到最大值:在最后一层上只缺少右边的若干结点. complete binary tree 满二叉树,完美二叉树是全部结点数达到最大的二叉树.perfect binary tree, full binary tree. 平衡二叉树,左右子树的高度在一定范围内. 二叉查找树(Binary Search Tree),也称二叉搜索树.有序二叉树(ordered binary tree),排序二叉树(sorted b…
3.1 Describe how you could use a single array to implement three stacks. Flexible Divisions的方案,当某个栈满了之后,需要把相邻的栈调整好,这是一个递归的过程. 每个stack有一些属性,所以不妨将每个stack封闭起来,我这里是用一个private的struct来实现,方便,同时对外部又不可见. 对于一些常用的操作,比如环形数组取下一个数,前一个数,都可以封装起来. class XNStack { pub…
链表的题里面,快慢指针.双指针用得很多. 2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this problem if a temporary buffer is not allowed? 2.2 Implement an algorithm to find the kth to last element of a singly linked list.…
8.2 Imagine you have a call center with three levels of employees: respondent, manager, and director. An incoming telephone call must be first allocated to a respondent who is free. If the respondent can't handle the call, he or she must escalate the…
7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only the add operator. 比较简单.但是要封装得好. 7.5 Given two squares on a two-dimensional plane, find a line that would cut these two squares in half. Assume that th…