Groupon面经:Find paths in a binary tree summing to a target value
You are given a binary tree (not necessarily BST) in which each node contains a value. Design an algorithm to print all paths which sum up to that value. Note that it can be any path in the tree - it does not have to start at the root.
Traverse through the tree from the root and do a post-order gathering of all path sums. Use a hashtable at each node to store the possible paths rooted at a node and going down-only. Key is the path sum, value is the actual path. We can construct all paths going through a node from itself and its childrens' paths.
Here is psuedo-code that implements the above, but stores only the sums and not the actual paths. For the paths themselves, you need to store the end node in the hashtable (we know where it starts, and there's only one path between two nodes in a tree).
function findsum(tree, target)
# Traverse the children
if tree->left
findsum(tree.left, target)
if tree->right
findsum(tree.right, target)
# Single node forms a valid path
tree.sums = {tree.value}
# Add this node to sums of children
if tree.left
for left_sum in tree.left.sums
tree.sums.add(left_sum + tree.value)
if tree.right
for right_sum in tree.right.sums
tree.sums.add(right_sum + tree.value)
# Have we formed the sum?
if target in tree.sums
we have a path
# Can we form the sum going through this node and both children?
if tree.left and tree.right
for left_sum in tree.left.sums
if target - left_sum in tree.right.sums
we have a path
# We no longer need children sums, free their memory
if tree.left
delete tree.left.sums
if tree.right
delete tree.right.sums
Groupon面经:Find paths in a binary tree summing to a target value的更多相关文章
- LintCode Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- leetcode : Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 / \2 ...
- Binary Tree Paths
Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...
- (easy)LeetCode 257.Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- 257. Binary Tree Paths
题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...
随机推荐
- thinkphp 设计思想
thinkphp 运行机制: thinkphp首先设置一些常量.然后运行Think对象(系统环境),装载think的运行环境,包括:自动加载.异常处理.功能核心等.环境准备好后,运行App对象(应用程 ...
- P1970 花匠
状态定义是dp中非常重要的,可以直接影响到效率,如此题,第一种思路是: #include <bits/stdc++.h> using namespace std; const int ma ...
- trunc sysdate
select * from per_all_people_f papf where trunc(sysdate) between trunc(papf.effective_start_date) a ...
- 初学MyBatis.net
1.MyBatis.net介绍 MyBatis..net是一个简单,但是完整的ORM框架,它使你的实体对象与sql语句或者存储过程之间的映射变得很简单,并提供数据访问.包括两个主要框架 DataAcc ...
- MVC3中几个小知识点
1.ViewBag.Name~ViewBag.name等价,即不区分大小写.在此小心,下次见到不要奇怪,不过最好还是写成一样的比较好. 2.JS字符串不允许有换行符,\'等字符,需提前处理.
- nrf51822裸机教程-PWM
先简单介绍一下PWM的原理. 原理很简单. 假设COUNTER是个从0开始递增的计数器. 我们设置两个值 counter0 和counter1 在 COUNTER 计数到counter0的值时候翻转 ...
- Nginx的配置中与流量分发相关的配置规范:
1.除首页外,其他页面都在某个目录中首页可以直接在根目录下,其他页面都要在根目录下的目录中.不同的location尽量使用第一个dir的模式进行区分,便于区分该流量是落在nginx本地,还是转发到后端 ...
- 关于cocoa框架,你所要知道的一切(苹果官方文档,cocoa框架核心竞争力,必须收藏!)
https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Accessib ...
- aspx后台页面添加服务器控件
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(); System.IO.StringWriter st ...
- Bundle文件的创建和使用(二)
1.概念: An NSBundle object represents a location in the file system that groups code and resources tha ...