栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

一道字节跳动的面试题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

一道字节跳动的面试题

 题目:有一个二叉树如下图所示,要求编程找出所有从根节点到叶节点路径总和等于22的路径。
          5
         /
      4    8
     /      /
  11    13  4
  /      /
7   2  5  1


#include 
#include 
#include 
using namespace std;

const int SUM = 22;

struct node {
	int value;
	struct node *left_child = nullptr, *right_child = nullptr;
	string paths = "";//父节点以上所有节点的value转string后拼接,split by ' '
	int sum = 0;//父节点以上所有节点的value加和
};

struct node *new_node(int value)
{
	struct node *p = new struct node;
	p->value = value;
	return p;
}

struct node *add_children(int parent_value, struct node *left_child, struct node *right_child)
{
	struct node *parent = new struct node;
	parent->value = parent_value;
	parent->left_child = left_child;
	parent->right_child = right_child;
	return parent;
}

// 通过遍历二叉树的方式(这里采用中序遍历,当然采用前序遍历和后序遍历也可以),当遍历到叶节点的时候,判断该条路径是否符合条件
void traverse_tree(struct node *parent)
{
	if (parent == nullptr)
		return;

	if (parent->left_child)
	{
		parent->left_child->paths = parent->paths + " " + to_string(parent->value);
		parent->left_child->sum = parent->sum + parent->value;
		traverse_tree(parent->left_child);
	}

	if (parent->left_child == nullptr && parent->right_child == nullptr) // 判断当前是否为叶节点
	{
		// 判断路径上的节点总和是否等于SUM
		if (parent->sum + parent->value == SUM)
			cout << "找到一条路径:" << parent->paths << " " << parent->value << endl;
	}

	if (parent->right_child)
	{
		parent->right_child->paths = parent->paths + " " + to_string(parent->value);
		parent->right_child->sum = parent->sum + parent->value;
		traverse_tree(parent->right_child);
	}
}

int main() {

	struct node *binary_tree = nullptr;

	// 生成左子树
	struct node *left_child = new_node(7);
	struct node *right_child = new_node(2);
	struct node *parent = nullptr;
	parent = add_children(11, left_child, right_child);
	parent = add_children(4, parent, nullptr);
	binary_tree = add_children(5, parent, nullptr);

	// 生成右子树
	left_child = new_node(5);
	right_child = new_node(1);
	parent = add_children(13, left_child, right_child);
	parent = add_children(8, parent, new_node(4));
	binary_tree->right_child = parent;

	// 找到目标和等于SUM的路径
	traverse_tree(binary_tree);

	return 0;
}

程序运行得到的结果为:

找到一条路径: 5 4 11 2

也许这里给出的程序实现不是最好的,但这也算是可行的一种解法吧!

如果题目再修改一下,变为“编程找出所有途经节点路径总和等于22的路径。”,程序的实现可以是下面这样子:


#include 
#include 
using namespace std;

const int SUM = 22;

struct node {
	int value;
	struct node *parent = nullptr, *left_child = nullptr, *right_child = nullptr;
};

struct node *new_node(int value)
{
	struct node *p = new struct node;
	p->value = value;
	return p;
}

void add_children(struct node *parent, struct node *left_child, struct node *right_child)
{
	if (left_child)
	{
		parent->left_child = left_child;
		left_child->parent = parent;
	}
	if (right_child)
	{
		parent->right_child = right_child;
		right_child->parent = parent;
	}
}

struct node *add_children(int parent_value, struct node *left_child, struct node *right_child)
{
	struct node *parent = new struct node;
	parent->value = parent_value;
	add_children(parent, left_child, right_child);
	return parent;
}

// 通过遍历二叉树的方式(这里采用中序遍历,当然采用前序遍历和后序遍历也可以)
void traverse_tree(struct node *parent)
{
	if (parent == nullptr)
		return;

	traverse_tree(parent->left_child);

	// 从当前节点往父节点回溯,判断该路径上的节点总和是否等于SUM;如果不等于,继续回溯,直到根节点
	string paths = "";
	int sum = 0;
	struct node *p = parent;
	while (p)
	{
		paths = to_string(p->value) + " " + paths;
		sum += p->value;
		if (sum == SUM)
		{
			cout << "找到一条路径:" << paths << endl;
			break;
		}
		p = p->parent;
	}

	traverse_tree(parent->right_child);
}

int main() {

	struct node *binary_tree = nullptr;

	// 生成左子树
	struct node *left_child = new_node(7);
	struct node *right_child = new_node(2);
	struct node *parent = nullptr;
	parent = add_children(11, left_child, right_child);
	parent = add_children(4, parent, nullptr);
	binary_tree = add_children(5, parent, nullptr);

	// 生成右子树
	left_child = new_node(5);
	right_child = new_node(1);
	parent = add_children(13, left_child, right_child);
	parent = add_children(8, parent, new_node(4));
	add_children(binary_tree, nullptr, parent);

	// 找到目标和等于SUM的路径
	traverse_tree(binary_tree);

	return 0;
}

程序运行的结果为:

找到一条路径:4 11 7
找到一条路径:5 4 11 2
找到一条路径:8 13 1

对这道题,如果你有更好的解法,欢迎在下方留言,一起讨论。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1007584.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号