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高手进阶:Adapter深入理解与优化

    一般是针对包含多个元素的View,如ListView,GridView,ExpandableListview,的时候我们是给其设置一个Adapter.Adapter是与View之间提供数据的桥梁,也是 ...

  2. JSON介绍与JavaScript解析

    首先什么是JSON? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式 JSON 独立于语言 JSON ...

  3. SQLSERVER 启用跨库查询脚本

    启用Ad Hoc Distributed Queries的方法,执行下面的查询语句就可以了: exec sp_configure 'show advanced options',1reconfigur ...

  4. Windows删除大文件

    Temp是目录 或者是 文件很大很大很大很大 cmd rd /s /q Temp

  5. web HTML5 调用摄像头的代码

    最近公司要求做一个在线拍照的功能,具体代码如下: <html> <head> <title>html5调用摄像头拍照</title> <style ...

  6. 20150528—html使用Jquery遍历text文本框的非空验证

    <script src="jquery-1.7.2.min.js" type="text/javascript"></script> & ...

  7. linux下开发板网络速度测试记录

        由于做的项目对于网络和USB的读写速度有很高的要求,因此新拿回来的板子要测试网络和usb的最佳传输速度.要考虑不少因素,先把我能想到的记录下来.     测试的环境是开发板和ubuntu虚拟机 ...

  8. ECC校验原理以及在Nand Flash中的应用

         本篇文章主要介绍ECC基本原理以及在Nand Flash中的应用,本文记录自己对ECC校验原理的理解和学习. ECC介绍      ECC,全称为Error Correcting Code, ...

  9. VS2010使用TeeChart5的ColorGrid绘制一维距离像

    绘制一维距离像原理:使用TeeChart控件中的ColorGrid显示(X,Y,Z)三维数据,X和Z分别代表一维距离像的x轴和y轴数据,Y代表对应的数值,以不同颜色显示. 1.注册TeeChart5 ...

  10. jeecms子栏目或者文章页导航父栏目选中解决方法

    jeecms在子栏目或者文章页导航父栏目选中,看例子 <div class="nav"> <ul> [@cms_channel_list ] <li ...