求二叉树的路径和(path sum)

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

Note: A leaf is a node with no children.

Example:

Given the below binary tree and sum = 22,

1234567
      5     /     4   8   /   /   11  13  4 /        7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.



看到这道题,嗯~

其实我锁定的本题的字眼在 “root-to-leaf”,这就是说明,至少你要从根节点一直遍历到叶节点为止。讲所有的叶节点遍历完之后,也就得到了多少个答案,然后从答案中筛选出预期的结果。

遍历的图比较好的方式一般使用递归的方式,因为迭代会使程序写法变得复杂~

12345678910111213141516171819202122232425262728293031323334353637383940414243
 * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class  {public:    void calcuSum(struct TreeNode* root, int sum) {      if (nullptr != root) {        if(nullptr!=root->left&&root->right==nullptr)          calcuSum(root->left, sum + root->val);        if(nullptr != root->right&&root->left==nullptr)          calcuSum(root-&g 大专栏  求二叉树的路径和(path sum)t;right, sum + root->val);        if (nullptr != root->left&&nullptr != root->right) {          calcuSum(root->left, sum + root->val);          calcuSum(root->right, sum + root->val);        }        if (nullptr == root->left&&nullptr == root->right) {          //or calcuSum(root->right, sum + root->val);          calcuSum(root->left, sum + root->val);        }      } else {        result.insert(sum);      }	  }    bool hasPathSum(TreeNode* root, int sum) {        if(nullptr==root){            return false;        }        calcuSum(root,0);        if(result.find(sum)!=result.end()){            return true;        }else{            return false;        }    }private:    set<int> result;};

根据鄙人的解题思路~ 写出上了以上代码~

上诉考虑到了容器的选择,我使用了set容器,主要是考虑从查找效率上。其实容器也可以选择vector,这个影响不是很大。



有了上诉题目的引子~

根据查找的结果,显示出路径来,题目如下:

Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.

Note: A leaf is a node with no children.

Example:

Given the below binary tree and sum = 22,

12345678910111213
      5     /     4   8   /   /   11  13  4 /      / 7    2  5   1Return:

[   [5,4,11,2],   [5,8,4,5]]

这道题和上一题一样的,不同的是,我们需要记录路径了,路径怎么记录~

这时候,我们可以使用容器vector<>,然后利用形参的copy作用,讲所有root-to-leaf路径存储起来,我们只需要将上面例子的int num,换成vector就行了,然后将这个值存储起来即可~~~

原文地址:https://www.cnblogs.com/lijianming180/p/12401954.html

时间: 2024-10-06 18:44:31

求二叉树的路径和(path sum)的相关文章

[Leetcode] Binary tree maximum path sum求二叉树最大路径和

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1 / 2 3 Return6. 思路:题目中说明起始节点可以是任意节点,所以,最大的路径和不一样要经过root,可以是左子树中某一条,或者是右子树中某一条,当然也可能是经过树的根节点root的.递归式是应该是这三者中

Java实现求二叉树的路径和

题: 解: 这道题考的是如何找出一个二叉树里所有的序列. 我的思路是先从根节点开始遍历,找出所有的子节点,因为每个子节点只有一个父节点,再根据每个子节点向上遍历找出所有的序列,再判断序列的总和. 这样解效率不高,但暂时只能想到这样.如果您有其余的解法,期望告知. 代码: 1 package com.lintcode; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.Enumera

【算法】求二叉树各路径结点之和并找出最大值的路径

说在前面的话 最近没事将大学里的<数据结构>(严蔚敏,吴伟民著)一书重拾温习,受益颇多,才发现工作之中诸多经验问题都找到了理论支撑. 当时觉得没用的书,现在只能嘲笑当时得多low... 现在依然很low... --! 事件背景 因实际工作中,遇到一个关于权重的问题,需要将数据关系中最大权重的路径找到,然后就想到了<数据结构>中的DFS... 此事勾起了我码砖的激情,让我本已平静的心再次荡漾... 为了简单说明这个问题,我就拿个二叉树的模型来叙述一下我要达成的目标 CASE Exam

leetcode 437 路径和 Path Sum III

1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 int pathSum(TreeNode* root

Path Sum 路径和

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t

Leetcode #124 Binary Tree Maximum Path Sum

题目链接:https://leetcode.com/problems/binary-tree-maximum-path-sum/ 题中要求 maxPathSum(TreeNode *root) 函数返回二叉树中最大的 "Path Sum". 而这个最大的 "Path Sum" 可以被分解为三部分之和: "左子树的根节点" 到 "左子树的某个节点" 的 最大 "Path Sum". "右子树的根节点

[LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1 / 2 3 Return 6. 这道求二叉树的最大路径和是一道蛮有难度的题,难就难在起始位置和结束位置可以为任意位置,我当然是又不会了,于是上网看看大神们的解法,看了很多人的都没太看明白,最后发现了网友Yu's C

Leetcode:Path Sum 二叉树路径和

Path Sum: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return

LeetCode OJ:Binary Tree Maximum Path Sum(二叉树最大路径和)

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root. For exampl