IT公司100题-tencent-打印所有高度为2的路径
问题描述:
[10 6 8]
[6 4 2]
[6 4 5]
[16 14 15]
[16 18 20]
[14 12 11]
#include <iostream>
#include "../utility/printCollection.h"
using namespace std; struct bsnode{
int data;
bsnode* left;
bsnode* right;
}; void add_bsnode(bsnode* &root, int value)
{
if(root == NULL)
{
bsnode* tmp = new bsnode();
tmp->data = value;
tmp->left = NULL;
tmp->right = NULL;
root = tmp;
return;
}
if(value > root->data)
add_bsnode(root->right, value);
else if(value < root->data)
add_bsnode(root->left, value);
else
cout << "duplicate value" << endl;
} void print_tree(bsnode* root)
{
if(root == NULL)
return;
if(root->left != NULL)
print_tree(root->left);
cout << root->data << " ";
if(root->right != NULL)
print_tree(root->right);
} void check_node(bsnode* root, int n, int &depth, vector<int> &path)
{
//如果当前深度>n,不需要继续访问
if(depth > n || root == NULL)
return; //检查前压栈当前节点,depth+1
path.push_back(root->data);
depth++; //如果当前深度=n
if(depth == n)
if(root->left == NULL && root->right == NULL)
{
cout << "path found: " << endl;
printCollection(path);
cout << endl;
} //如果当前深度<n
if(depth < n)
if(root->left != NULL)
check_node(root->left, n, depth, path);
if(root->right != NULL)
check_node(root->right, n, depth, path); //检查完出栈当前节点,depth-1
path.pop_back();
depth--;
return;
} void print_node(bsnode* root, int n)
{
if(root == NULL)
return; //每次检查前重定义depth
int depth = -;
vector<int> path; //每次检查前clear path
path.clear(); //检查当前节点的所有叶子节点的高度
check_node(root, n, depth, path); //递归检查左子树
print_node(root->left, n);
//递归检查右子树
print_node(root->right, n);
} int main()
{
bsnode* root = NULL;
add_bsnode(root, );
add_bsnode(root, );
add_bsnode(root, );
add_bsnode(root, );
add_bsnode(root, );
add_bsnode(root, );
add_bsnode(root, );
add_bsnode(root, );
add_bsnode(root, );
add_bsnode(root, );
add_bsnode(root, );
add_bsnode(root, );
add_bsnode(root, ); cout << "Tree content: " << endl;
print_tree(root);
cout << endl << endl; int height = ;
cout << "Nodes that with height " << height << " are:" << endl;
print_node(root, height);
cout << endl;
}
输出:
$ ./a.exe
Tree content: Nodes that with height are:
path found:
[ ] path found:
[ ] path found:
[ ] path found:
[ ] path found:
[ ] path found:
[ ]
IT公司100题-tencent-打印所有高度为2的路径的更多相关文章
- IT公司100题-32-交换元素,使数组差最小
问题描述: 有两个整数序列a, b,大小都为n, 序列元素的值任意整数,无序. 要求:通过交换a, b 中的元素,使得sum(a)-sum(b),差最小. 例如: var a=[80, 40, 60, ...
- IT公司100题-21-输入n和m,和等于m
问题描述: 输入两个整数n 和m,从数列1,2,3,…,n 中随意取几个数, 使其和等于m,将所有可能的组合都打印出来. 分析: 利用递归的思路,对于1,2,3,…,n 中的任意一个数,要么选,要 ...
- IT公司100题-16-层遍历二元树
问题描述: 层遍历二叉树,同一层从左往右打印. 定义二元查找树的结点为: typedef struct BSTreeNode { int data; BSTreeNode *left; BSTreeN ...
- IT公司100题-15-求二元查找树的镜像
问题描述: 输入一颗二元查找树,将该树转换为它的镜像树,即对每一个节点,互换左右子树. 例如输入: 6/ \4 12/ \ / \2 5 8 16 输出: 6/ ...
- IT公司100题-4-在二元树中找出和为某一值的所有路径
问题描述: 输入一个整数和一棵二元树.从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径.打印出和与输入整数相等的所有路径. 例如输入整数30和如下二元树 14 / \ 5 16 / ...
- IT公司100题-1-二叉树转换为双链表
问题描述: 输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表.要求不能创建任何新的结点,只调整指针的指向. 10 / \ 6 14/ \ / \4 8 1 ...
- IT公司100题-35- 求一个矩阵中最大的二维矩阵(元素和最大)
问题描述: 求一个矩阵中最大的二维矩阵(元素和最大).如: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 中最大的是: 4 5 9 10 分析: 2*2子数组的最大和.遍历求和,时 ...
- IT公司100题-28-整数的二进制表示中1的个数
问题描述: 输入一个整数n,求n的二进制表示中,一共有多少个1.例如n=8,二进制表示为00001000,二进制表示中有1个1. 分析: 如果一个数n不为0,那么n-1的二进制表示,与n的二进 ...
- IT公司100题-27-跳台阶问题
问题描述: 一个台阶总共有n阶,一次可以跳1级或者2级.求总共有多少种跳法. 分析: 用f(n)表示n阶台阶总共有多少种跳法.n阶台阶,第一可以选择跳1阶或者2阶,则f(n) = f(n-1) + ...
随机推荐
- -XX:-PrintClassHistogram 按下Ctrl+Break后,打印类的信息
-XX:+PrintClassHistogram –按下Ctrl+Break后,打印类的信息: num #instances #bytes class name ------ ...
- 封装Js库从获取控件的value值开始
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...
- SQL递归查询(with cte as)
with cte as ( select Id,Pid,DeptName,0 as lvl from Department where Id = 2 union all select d.Id,d.P ...
- 【转】Struts1.x系列教程(5):HTML标签库
转载地址:http://www.blogjava.net/nokiaguy/archive/2009/01/archive/2009/01/archive/2009/01/archive/2009/0 ...
- 结对编程—黄金点游戏WinForm单机版
本小游戏场景来自邹欣老师的<移山之道>一书: "阿超的课都是下午两点钟,这时班上不少的同学都昏昏欲睡,为了让大家兴奋起来,阿超让同学玩一个叫"黄金点"的游戏: ...
- Theme Section(KMP应用 HDU4763)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 一步一步教你编写与搭建自动化测试框架——python篇
[本文出自天外归云的博客园] 这两天用python写了一个自动化测试框架,取名为Auty.准备用来做Web方面的接口测试,以下为Auty框架一步一步的搭建过程——
- Cheatsheet: 2015 05.01 ~ 05.31
.NET .NET on Mac for the OS X n00b without Mono via Visual Studio Code Microsoft frameworks deprecat ...
- (转) vector的reserve和resize
文章转自 http://www.cnblogs.com/qlee/archive/2011/05/16/2048026.html vector 的reserve增加了vector的capacity, ...
- 深入浅出设计模式——备忘录模式(Memento Pattern)
模式动机 为了使软件的使用更加人性化,对于误操作,我们需要提供一种类似“后悔药”的机制,让软件系统可以回到误操作前的状态,因此需要保存用户每一次操作时系统的状态,一旦出现误操作,可以把存储的历史状态取 ...