二叉树中和为某一值的路径
来源:互联网 发布:区县网络社会组织调研 编辑:程序博客网 时间:2024/06/07 02:08
开始想的是按照求最大值路径的方法,来求出每一条路径的值,后来发现没办法自底向上求出每一条路的值。
int max_path(BitTree b) { if (b == nullptr) return 0; int temp1 = b->data + max_path(b->left); int temp2 = b->data + max_path(b->right); return (temp1 > temp2) ? temp1 : temp2;}
bool find_max_path(BitTree b, int len) { void find_max_path(BitTree, int, int, vector<BitTree>&); if (b == nullptr) return false; vector<BitTree> v; find_max_path(b, len, 0, v); return true;}//用一个vector来保存遍历过的节点void find_max_path(BitTree b, int len, int Curren_len, vector<BitTree> &v) { Curren_len += b->data; v.push_back(b); bool is_n = (b->left == nullptr) && (b->right == nullptr); if (Curren_len == len && is_n) { auto i = v.begin(); while (i!=v.end()) { cout << (*i)->data << " "; i++; } cout << endl; } if(b->left!=nullptr) find_max_path(b->left, len, Curren_len, v); if(b->right!=nullptr) find_max_path(b->right, len, Curren_len, v); v.pop_back();}
阅读全文
0 0
- 【树】二叉树中和为某一值的路径
- 【树6】二叉树中和为某一值的路径
- 题目11:二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的所有路径
- 题目1368:二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 1368:二叉树中和为某一值的路径 @jobdu
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- Q25:二叉树中和为某一值的路径
- 剑指offer:二叉树中和为某一值的路径
- 二叉树中和为某一值的路径(剑指offer25)
- 01 初入数字图像处理感想
- Redis服务器
- java企业级
- [easyx游戏开发]---实现透明背景效果
- SCROLLVIEW MATCH_PARENT_SCROLLVIEW无法填充满屏幕
- 二叉树中和为某一值的路径
- HDU 1016-Prime Ring Problem
- 如何打开pr_debug调试信息
- tomcat修改默认页面路径为自己项目路径
- 刚接触安卓和eclipse时的遇到的坎儿
- Python 列表(List)操作方法详解(转载)
- C# WinForm利用sideBar动态加载菜单栏,并创建菜单栏事件
- C# 获取电脑名IP地址及当前用户名
- 了解SQL中 inner join、 left join 、right join、 outer join之间的区别