Careercup - Facebook面试题 - 5344154741637120
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的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
- Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
- Careercup - Facebook面试题 - 5188884744896512
2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'n ...
随机推荐
- 微软推出的免费新书《Introducing Microsoft SQL Server 2012》
微软推出的免费新书<Introducing Microsoft SQL Server 2012>,该书详细介绍微软SQL 2012数据库服务最新功能以及功能应用和使用技巧. 该书适合SQL ...
- 【转载】php程序员:从1.5K到18K 一个程序员的5年成长之路
昨天收到了心仪企业的口头offer, 回首当初什么都不会开始学编程, 到现在恰好五年. 整天在社区晃悠, 看了不少的总结, 在这个时间点, 我也写一份自己的总结吧. 我一直在社区分享, 所以, 这篇总 ...
- MVC 开启gzip压缩
using System.IO; using System.IO.Compression; using System.Web; using System.Web.Mvc; public class C ...
- 利用ExcelDataReader封装类 导入表格数据
nuget 添加Install-Package ExcelDataReader
- linux 环境变量的设置【转】
现在使用linux的朋友越来越多了,在linux下做开发首先就是需要配置环境变量,下面以java环境变量为例介绍三种配置环境变量的方法. 1.修改/etc/profile文件 如果你的计算机仅仅作为开 ...
- (转)分布式缓存GemFire架构介绍
1什么是GemFire GemFire是一个位于应用集群和后端数据源之间的高性能.分布式的操作数据(operational data)管理基础架构.它提供了低延迟.高吞吐量的数据共享和事件分发.Gem ...
- [Bootstrap]组件(一)
Glyphicons字体图标 基类.glyphicon {position/top/display/font-family/} 具体类 {content} --表现在伪元素上 使用要点:a.基类 ...
- SpringMvc入门二----HelloWorld
1. 导入需要的架包: 2. 配置web.xml,添加Servlet <servlet> <servlet-name>springmvc</servlet-name> ...
- php var_export与var_dump 输出的不同
var_export必须返回合法的php代码,var_export返回的代码,可以直接当作php代码赋值个一个变量. 而这个变量就会取得和被var_export一样的类型的值. 问题描述: 在跟踪 ...
- 配置Windows 2008 R2 64位 Odoo 8.0/9.0 源码开发调试环境
安装过程中,需要互联网连接下载python依赖库: 1.安装: Windows Server 2008 R2 x64标准版 2.安装: Python 2.7.10 amd64 到C:\Python27 ...