二叉查换树,左孩子小于等于根,右孩子大于根。

完全二叉树,除最后一层外,每一层上的节点数均达到最大值;在最后一层上只缺少右边的若干结点。 complete binary tree

满二叉树,完美二叉树是全部结点数达到最大的二叉树。perfect binary tree, full binary tree.

平衡二叉树,左右子树的高度在一定范围内。

二叉查找树(Binary Search Tree),也称二叉搜索树、有序二叉树(ordered binary tree),排序二叉树(sorted binary tree),是指一棵空树或者具有下列性质的二叉树:

  1. 若任意节点的左子树不空,则左子树上所有结点的值均小于或等于它的根结点的值;
  2. 任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
  3. 任意节点的左、右子树也分别为二叉查找树。
  4. 没有键值相等的节点(no duplicate nodes)。

在二叉查找树删去一个结点,分三种情况讨论:

  • 若*p结点为叶子结点,即PL(左子树)和PR(右子树)均为空树。由于删去叶子结点不破坏整棵树的结构,则只需修改其双亲结点的指针即可。
  • 若*p结点只有左子树PL或右子树PR,此时只要令PL或PR直接成为其双亲结点*f的左子树(当*p是左子树)或右子树(当*p是右子树)即可,作此修改也不破坏二叉查找树的特性。
  • 若*p结点的左子树和右子树均不空。在删去*p之后,为保持其它元素之间的相对位置不变,可按中序遍历保持有序进行调整,可以有两种做法:其一是令*p的左子树为*f的左/右(依*p是*f的左子树还是右子树而定)子树,*s为*p左子树的最右下的结点,而*p的右子树为*s的右子树;其二是令*p的直接前驱(in-order predecessor)或直接后继(in-order successor)替代*p,然后再从二叉查找树中删去它的直接前驱(或直接后继)。

4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one.

4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes.

BFS或者DFS就行了,不过BFS可以避免在单条路径上挖得太深。

4.3 Given a sorted (increasing order) array with unique integer elements, write an algorithm to create a binary search tree with minimal height

4.4 Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth (e.g., if you have a tree with depth D, you'll have D linked lists).

BFS或者DFS都可以。注意的是,DFS虽然会用额外的O(lgn)的栈空间,但是整体的空间复杂度还是O(n)。

4.5 Implement a function to check if a binary tree is a binary search tree.

。BST要和中序遍历联系在一起。

4.6  Write an algorithm to find the 'next'node (i.e., in-order successor) of a given node in a binary search tree. You may assume that each node has a link to its parent.

如果有root结点,直接用中序遍历记录pre结点就可以做的。如果只有结点,那么需要分情况。

4.7 Design an algorithm and write code to find the first 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.

leetcode上的博文,讲得很仔细。这道题很经典,尤其是有parent结点时的解法,类似求intersect linklist的交叉点。

4.8 You have two very large binary trees: Tl, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of Tl. A tree T2 is a subtree of Tl if there exists a node n in Tl such that the subtree of n is identical to T2. That is, if you cut off the tree at node n, the two trees would be identical.

这道题可以穷搜,也可以基于pre-order和in-order的travesal串,用子串查找。

4.9 You are given a binary tree in which each node contains a value. Design an algorithm to print all paths which sum to a given value. The path does not need to start or end at the root or a leaf.

只能穷搜了。

 struct TreeNode {
TreeNode *left;
TreeNode *right;
TreeNode *parent;
int val;
TreeNode(int v):val(v),left(NULL),right(NULL),parent(NULL) {}
}; // 4.6
TreeNode* findInorderNext(TreeNode* node) {
if (node == NULL) {
return NULL;
} else if (node->right) {
node = node->right;
while (node->left) node = node->left;
return node;
} else {
while (node->parent && node->parent->left != node) node = node->parent;
return node->parent;
}
} TreeNode* findPreordeNext(TreeNode* node) {
if (node == NULL) {
return NULL;
} else if (node->left) {
return node->left;
} else if (node->right) {
return node->right;
} else {
while (node->parent && node->parent->right == node) {
node = node->parent;
}
if (node->parent) return node->parent->right;
else return NULL;
}
} TreeNode* findPostorderNext(TreeNode* node) {
if (node == NULL || node->parent == NULL) {
return NULL;
} else if (node->parent->right == node || node->parent->right == NULL) { // right subtree
return node->parent;
} else {
node = node->parent->right;
while (node->left) node = node->left;
return node;
}
} // 4.7 (1) p, q in the BST, O(h) runtime
TreeNode* LCAInBST(TreeNode *root, TreeNode *p, TreeNode *q) {
if (!root || !p || !q) return NULL;
if (max(p->val, q->val) < root->val) {
return LCAInBST(root->left, p, q);
} else if (min(p->val, q->val) > root->val) {
return LCAInBST(root->right, p, q);
} else {
return root;
}
} // 4.7 (2.1) p, q may not in the tree, this is just a binary tree
int matchCount(bool pExisted, bool qExisted) {
int m = ;
if (pExisted) m++;
if (qExisted) m++;
return m;
}
TreeNode* LCAInBT(TreeNode *root, TreeNode *p, TreeNode *q, bool &pExisted, bool &qExisted) {
if (!root) return NULL; TreeNode* ret = LCAInBT(root->left, p, q, pExisted, qExisted);
int lm = matchCount(pExisted, qExisted);
if (lm == ) return ret; // p, q are in the left subtree
ret = LCAInBT(root->right, p, q, pExisted, qExisted);
int rm = matchCount(pExisted, qExisted);
if (rm == ) { // p, q are found
if (lm == ) return root; //p, q are in different subtree, thus return root
else return ret; // lm == 0, p, q are in the right subtree
}
if (root == p) pExisted = true;
if (root == q) qExisted = true;
if (pExisted && qExisted) return root; // if q is a child of q (or, q is a child of p)
return NULL; // if p or q is not existed
} // 4.7(2.2) the parent node is available
int getHeight(TreeNode* p) {
int h = ;
while (p) {
p = p->parent;
h++;
}
return h;
} TreeNode* LCAInBT(TreeNode *p, TreeNode *q) {
if (!p || !q) return NULL;
int h1 = getHeight(p);
int h2 = getHeight(q);
if (h1 > h2) {
swap(p, q);
swap(h1, h2);
}
for (int i = ; i < h2 - h1; ++i) {
q = q->parent;
} while (p && q) {
if (p == q) return p;
p = p->parent;
q = q->parent;
}
return NULL;
}

Careercup | Chapter 4的更多相关文章

  1. Careercup | Chapter 1

    1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot us ...

  2. Careercup | Chapter 3

    3.1 Describe how you could use a single array to implement three stacks. Flexible Divisions的方案,当某个栈满 ...

  3. Careercup | Chapter 2

    链表的题里面,快慢指针.双指针用得很多. 2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow w ...

  4. Careercup | Chapter 8

    8.2 Imagine you have a call center with three levels of employees: respondent, manager, and director ...

  5. Careercup | Chapter 7

    7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only ...

  6. CareerCup Chapter 9 Sorting and Searching

    9.1 You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. ...

  7. CareerCup chapter 1 Arrays and Strings

    1.Implement an algorithm to determine if a string has all unique characters What if you can not use ...

  8. CareerCup Chapter 4 Trees and Graphs

    struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int val):val(val),left(NULL),rig ...

  9. Careercup | Chapter 6

    6.2 There is an 8x8 chess board in which two diagonally opposite corners have been cut off. You are ...

  10. Careercup | Chapter 5

    5.1 You are given two 32-bit numbers, N andM, and two bit positions, i and j. Write a method to inse ...

随机推荐

  1. matplotlib学习记录 一

    from matplotlib import pyplot as plt # 先实例一个图片,传入图片参数,10宽,5高,分辨率为80 image = plt.figure(figsize=(10,5 ...

  2. Pond Cascade Gym - 101670B 贪心+数学

    题目:题目链接 思路:题目让求最下面池子满的时间和所有池子满的时间,首先我们考虑所有池子满的时间,我们从上到下考虑,因为某些池子满了之后溢出只能往下溢水,考虑当前池子如果注满时间最长,那么从第一个池子 ...

  3. kettle-单表增量同步

    目标:利于kettle实现单表增量同步,以时间为判断条件 背景:源表:db1.q1 (2w条数据) 目标表:db2.q2(0条数据) 表结构: CREATE TABLE `q1` (  `ID` bi ...

  4. P2014 选课(树形背包)

    P2014 选课 题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有 ...

  5. 小x的质数(线性O(n)筛素数)

    小x的质数 题目描述 小 X 是一位热爱数学的男孩子,在茫茫的数字中,他对质数更有一种独特的情感.小 X 认为,质数是一切自然数起源的地方. 在小 X 的认知里,质数是除了本身和 11 以外,没有其他 ...

  6. asynctask 异步下载

    public class MainActivity extends Activity{ private TextView show; @Override public void onCreate(Bu ...

  7. Windows清理打印池的方法

    另存为bat运行   @echo off title 快速清除打印队列 echo. echo 停止打印机服务 net stop spooler>nul echo. del /q /f %wind ...

  8. jenkins 之 Android 打包及上传至蒲公英

    准备条件 iMAC,非必须(如果是 安卓 和 苹果 可以在同一台电脑上打包则要 Mac OS 系统的电脑,如果是只是给安卓打包 windows 电脑也是可以的, window 下 需要把 ls 换成 ...

  9. splay模板三合一 luogu2042 [NOI2005]维护数列/bzoj1500 [NOI2005]维修数列 | poj3580 SuperMemo | luogu3391 【模板】文艺平衡树(Splay)

    先是维修数列 题解看这里,但是我写的跑得很慢 #include <iostream> #include <cstdio> using namespace std; int n, ...

  10. # Linux 命令学习记录

    Linux 命令学习记录 取指定文件夹下的任意一个文件,并用vim打开 vi $(ls -l|grep "^-"|head -n 1|awk '{print $9}') 统计给定文 ...