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. Android 手势滑动,多点触摸放大缩小图片

    效果展示: 基本思路: <1>首先写一个图片控制类ImageControl,实现对图片控制的的基本操作,我们的图片控制类ImageControl是继承自ImageView自定义的视图: & ...

  2. Part 95 to 96 Deadlock in a multithreaded program

    Part 95   Deadlock in a multithreaded program class Program { static void Main(string[] args) { Cons ...

  3. XenApp简单部署

    作者:MR.Yangwj 目录 XenApp简单部署... 1 一.         XenApp安装... 1 (一)      服务器配置任务... 9 1)     许可证服务器配置... 9 ...

  4. dateset是不是在缓存中

    C#开发erp系统的时候有一个多表数据的查询展示到页面,采用了存储过程的方式,但是存储过程中没有加入分页(菜比).刚开始测试数据几百条没有问题,当数据量提升至十万级后页面加载速度就很卡了,一般是使用分 ...

  5. UI2_QQ折叠-UITableViewController

    // CustomUITableViewController.h // UI2_QQ折叠-UITableViewController // // Created by zhangxueming on ...

  6. 济南学习 Day 5 T1 pm

    欧拉函数(phi)题目描述: 已知(N),求phi(N). 输入说明: 正整数N. 输出说明: 输出phi(N). 样例输入: 8 样例输出: 4 数据范围: 对于20%的数据,N<=10^5 ...

  7. Codevs 2627 村村通

    时间限制: 1 s   空间限制: 32000 KB  题目等级 : 黄金 Gold  题目描述 Description 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到 ...

  8. Linux I/O模型

    同步阻塞I/O 在此种方式下,用户进程在发起一个I/O操作以后,必须等待I/O操作的完成,只有当真正完成了I/O操作以后,用户进程才能运行.Java传统的I/O模型属于此种方式. 同步非阻塞I/O 在 ...

  9. iOS开发笔记-两种单例模式的写法

    iOS开发笔记-两种单例模式的写法   单例模式是开发中最常用的写法之一,iOS的单例模式有两种官方写法,如下: 不使用GCD #import "ServiceManager.h" ...

  10. 《LDAP服务器的配置与客户端的测试》RHEL6——第一篇 运维工程师必考

    ldap这种原始的服务器搭建起来比较复杂,同时它也是CE必考的(客户端的搭建). 服务器端的配置: 1.安装openldap-servers软件包 2.查看ldap模板文件的存放位置: 3.拷贝lda ...