2014-05-02 10:40

题目链接

原题:

Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of one of its descendants
so that no node with value zero could be parent of node with non-zero.

题目:把二叉树中的值为0的节点尽量往下沉,保证所有值为0的节点绝不会有非0的子节点。

解法:我写的算法是O(n^2)的,对于每个值为0的节点,都向下寻找值为非0的节点,如果找不到,就说明没法下沉了;否则继续下沉。

代码:

 // http://www.careercup.com/question?id=5344154741637120
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
using namespace std; struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int _val = ): val(_val), left(nullptr), right(nullptr) {};
}; void constructBinaryTree(TreeNode *&root)
{
static string s;
stringstream sio;
int val; if (cin >> s && s == "#") {
root = nullptr;
} else {
sio << s;
if (sio >> val) {
root = new TreeNode(val);
constructBinaryTree(root->left);
constructBinaryTree(root->right);
} else {
root = nullptr;
}
}
} void preorderTraversal(TreeNode *root)
{
if (root == nullptr) {
cout << "# ";
} else {
cout << root->val << ' ';
preorderTraversal(root->left);
preorderTraversal(root->right);
}
} class Solution {
public:
void sinkZero(TreeNode *root) {
if (root == nullptr) {
return;
} if (root->val == ) {
TreeNode *ptr = findNonZeroNode(root); if (ptr != nullptr) {
swap(root->val, ptr->val);
} else {
// all zero, no need to go down any more.
return;
}
}
sinkZero(root->left);
sinkZero(root->right);
};
private:
TreeNode *findNonZeroNode(TreeNode *root) {
if (root == nullptr) {
return root;
} else if (root->val != ) {
return root;
} else {
TreeNode *ptr = findNonZeroNode(root->left);
if (ptr != nullptr) {
return ptr;
} else {
return findNonZeroNode(root->right);
}
}
};
}; int main()
{
TreeNode *root;
Solution sol; while (true) {
constructBinaryTree(root);
if (root == nullptr) {
break;
}
sol.sinkZero(root);
preorderTraversal(root);
cout << endl;
} return ;
}

Careercup - Facebook面试题 - 5344154741637120的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  3. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  4. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  5. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  6. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  7. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  8. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

  9. Careercup - Facebook面试题 - 5188884744896512

    2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'n ...

随机推荐

  1. 反射调用与Lambda表达式调用

    想调用一个方法很容易,直接代码调用就行,这人人都会.其次呢,还可以使用反射.不过通过反射调用的性能会远远低于直接调用——至少从绝对时间上来看的确是这样.虽然这是个众所周知的现象,我们还是来写个程序来验 ...

  2. DataTable转CSV

    直接上方法: /// <summary> /// 将DataTable转换成CSV文件 /// </summary> /// <param name="dt&q ...

  3. Sql Server 数据库之间如何进行跨网远程连接访问

    场景说明 现在有一台A电脑和一台B电脑,两台电脑都安装了Sql Server数据库,两台电脑不在一个局域网(我们考虑的是不同网络的两台数据库连接),比如A电脑在公司,B电脑在家里,现在我要在家里用B电 ...

  4. javascript中ajax post实例详解

    一,原生态的XMLHttpRequest 代码如下 复制代码 <script language="javascript">         function savei ...

  5. 【学习笔记】【C语言】逻辑运算符

    有时候,我们需要在多个条件同时成立的时候才能执行某段代码,比如:用户只有同时输入了QQ和密码,才能执行登录代码,如果只输入了QQ或者只输入了密码,就不能执行登录代码.这种情况下,我们就要借助于C语言提 ...

  6. 【转义字符】HTML 字符实体&lt; &gt: &amp;等

    在开发中遇到javascript从后台获取的url 会被转义,如:http://localhost:8080/Home/Index?a=14&b=15&c=123,想把它转成http: ...

  7. Nginx集群(转)

    转自:http://hi.baidu.com/xingyuanju/blog/item/779a2a23b7ebb749935807f1.html http://hi.baidu.com/dianhu ...

  8. (转)Yale CAS + .net Client 实现 SSO(1)

    由于信息系统集成需要,最近研究了一下CAS.从网上找了不少资料,很多是针对Java平台的,为数不多的针对.net Client的文章往往片面的介绍某个方面,照着去做确会遇到大量的问题,特别是“重定向循 ...

  9. SSH连接 NAT型 VirtualBox + LINUX

    1.首先登录到虚拟机中的Linux系统,查看一下使用NAT网卡的IP地址. 2.关闭虚拟机. 3.依次点击 "设置 -> 网络 -> (网络地址转换)端口转发",其中需 ...

  10. c/c++面试总结(2)

    4.深拷贝和浅拷贝 (1)什么时候会用到拷贝函数 一个对象以值传递的方式传入函数(就是作为入参) 一个对象以值传递的方式从函数返回(就是作为返回值) 一个对象需要通过另外一个对象进行初始化 (2)是否 ...