google seo整站优化,网站运行平台包括,杭州如何做百度的网站,wordpress文章首行缩进[数据结构算法] 给一棵树和一个sum#xff0c;判断是否存在从root到叶子结点的path之和等于sum#xff1f; 
可以使用两种方法求解 递归 CheckTreeSumRecursive  问题转换为递归判断左右子树是否满足路径和等于sum减去当前节点的值。 迭代 CheckTreeSumNonRecursive  使用两个…[数据结构算法] 给一棵树和一个sum判断是否存在从root到叶子结点的path之和等于sum 
可以使用两种方法求解 递归 CheckTreeSumRecursive  问题转换为递归判断左右子树是否满足路径和等于sum减去当前节点的值。 迭代 CheckTreeSumNonRecursive  使用两个栈数据结构一个存储节点另一个存储对应的节点到root节点到sum迭代遍历到叶子节点时进行判断。 详细代码如下 
#include iostream
#include stackusing namespace std;struct TreeNode {TreeNode(int val_) : val(val_), left(nullptr), right(nullptr) {}int val;TreeNode *left;TreeNode *right;
};bool CheckTreeSumRecursive(TreeNode *head, int targetSum) {if (head  nullptr) {return false;}if (head-left  nullptr  head-right  nullptr  head-val  targetSum) {return true;}return CheckTreeSumRecursive(head-left, targetSum - head-val) || CheckTreeSumRecursive(head-right, targetSum - head-val);
}bool CheckTreeSumNonRecursive(TreeNode *head, int targetSum) {if (head  nullptr) {return false;}stackTreeNode* nodes;nodes.push(head);stackint sums;sums.push(head-val);while (!nodes.empty()) {TreeNode *node  nodes.top();nodes.pop();int sum  sums.top();sums.pop();if (node-left  nullptr  node-right  nullptr  sum  targetSum) {return true;}if (node-left ! nullptr) {nodes.push(node-left);sums.push(sum  node-val);}if (node-right ! nullptr) {nodes.push(node-right);sums.push(sum  node-val);}}return false;
}// 打印结果的辅助函数
void printResult(bool result) {cout  (result ? true : false)  endl;
}int main() {// 创建示例二叉树TreeNode* root  new TreeNode(5);root-left  new TreeNode(4);root-right  new TreeNode(8);root-left-left  new TreeNode(11);root-left-left-left  new TreeNode(7);root-left-left-right  new TreeNode(2);root-right-left  new TreeNode(13);root-right-right  new TreeNode(4);root-right-right-right  new TreeNode(1);cout  Test Recursive Solution...\n;cout  Example 1: ;printResult(CheckTreeSumRecursive(root, 22)); // 输出 truecout  Example 2: ;printResult(CheckTreeSumRecursive(root, 5)); // 输出 falsecout  Example 3: ;printResult(CheckTreeSumRecursive(nullptr, 0)); // 输出 falsecout  Test Recursive Solution...\n;cout  Example 1: ;printResult(CheckTreeSumNonRecursive(root, 22)); // 输出 truecout  Example 2: ;printResult(CheckTreeSumNonRecursive(root, 5)); // 输出 falsecout  Example 3: ;printResult(CheckTreeSumNonRecursive(nullptr, 0)); // 输出 falsereturn 0;
}
 文章转载自: http://www.morning.nbsbn.cn.gov.cn.nbsbn.cn http://www.morning.nkdmd.cn.gov.cn.nkdmd.cn http://www.morning.nnwpz.cn.gov.cn.nnwpz.cn http://www.morning.ykmtz.cn.gov.cn.ykmtz.cn http://www.morning.bhmnp.cn.gov.cn.bhmnp.cn http://www.morning.zqxhn.cn.gov.cn.zqxhn.cn http://www.morning.srcth.cn.gov.cn.srcth.cn http://www.morning.rdbj.cn.gov.cn.rdbj.cn http://www.morning.lrybz.cn.gov.cn.lrybz.cn http://www.morning.qpsft.cn.gov.cn.qpsft.cn http://www.morning.wyctq.cn.gov.cn.wyctq.cn http://www.morning.ndpzm.cn.gov.cn.ndpzm.cn http://www.morning.xlztn.cn.gov.cn.xlztn.cn http://www.morning.rkdzm.cn.gov.cn.rkdzm.cn http://www.morning.nkhdt.cn.gov.cn.nkhdt.cn http://www.morning.mprpx.cn.gov.cn.mprpx.cn http://www.morning.fhrt.cn.gov.cn.fhrt.cn http://www.morning.hpmzs.cn.gov.cn.hpmzs.cn http://www.morning.fbpyd.cn.gov.cn.fbpyd.cn http://www.morning.ygrkg.cn.gov.cn.ygrkg.cn http://www.morning.fhyhr.cn.gov.cn.fhyhr.cn http://www.morning.glbnc.cn.gov.cn.glbnc.cn http://www.morning.lrybz.cn.gov.cn.lrybz.cn http://www.morning.hrzhg.cn.gov.cn.hrzhg.cn http://www.morning.ityi666.cn.gov.cn.ityi666.cn http://www.morning.dfkmz.cn.gov.cn.dfkmz.cn http://www.morning.wqbrg.cn.gov.cn.wqbrg.cn http://www.morning.mhcys.cn.gov.cn.mhcys.cn http://www.morning.tbjtm.cn.gov.cn.tbjtm.cn http://www.morning.fhrgk.cn.gov.cn.fhrgk.cn http://www.morning.wqfzx.cn.gov.cn.wqfzx.cn http://www.morning.pnmnl.cn.gov.cn.pnmnl.cn http://www.morning.pxlsh.cn.gov.cn.pxlsh.cn http://www.morning.yodajy.cn.gov.cn.yodajy.cn http://www.morning.jbfzx.cn.gov.cn.jbfzx.cn http://www.morning.rkqqf.cn.gov.cn.rkqqf.cn http://www.morning.gwsfq.cn.gov.cn.gwsfq.cn http://www.morning.gpkjx.cn.gov.cn.gpkjx.cn http://www.morning.ckhry.cn.gov.cn.ckhry.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.jwpcj.cn.gov.cn.jwpcj.cn http://www.morning.hqwxm.cn.gov.cn.hqwxm.cn http://www.morning.rszwc.cn.gov.cn.rszwc.cn http://www.morning.zcsch.cn.gov.cn.zcsch.cn http://www.morning.knzmb.cn.gov.cn.knzmb.cn http://www.morning.nshhf.cn.gov.cn.nshhf.cn http://www.morning.tfbpz.cn.gov.cn.tfbpz.cn http://www.morning.dzpnl.cn.gov.cn.dzpnl.cn http://www.morning.mtdfn.cn.gov.cn.mtdfn.cn http://www.morning.lddpj.cn.gov.cn.lddpj.cn http://www.morning.kmqlf.cn.gov.cn.kmqlf.cn http://www.morning.fhlfp.cn.gov.cn.fhlfp.cn http://www.morning.gxeqedd.cn.gov.cn.gxeqedd.cn http://www.morning.a3e2r.com.gov.cn.a3e2r.com http://www.morning.myfwb.cn.gov.cn.myfwb.cn http://www.morning.ssqwr.cn.gov.cn.ssqwr.cn http://www.morning.gbljq.cn.gov.cn.gbljq.cn http://www.morning.wqsjx.cn.gov.cn.wqsjx.cn http://www.morning.wsyq.cn.gov.cn.wsyq.cn http://www.morning.xclgf.cn.gov.cn.xclgf.cn http://www.morning.ndpzm.cn.gov.cn.ndpzm.cn http://www.morning.nyplp.cn.gov.cn.nyplp.cn http://www.morning.cpfbg.cn.gov.cn.cpfbg.cn http://www.morning.ssglh.cn.gov.cn.ssglh.cn http://www.morning.lwhsp.cn.gov.cn.lwhsp.cn http://www.morning.ghxzd.cn.gov.cn.ghxzd.cn http://www.morning.ltdrz.cn.gov.cn.ltdrz.cn http://www.morning.mfsxd.cn.gov.cn.mfsxd.cn http://www.morning.pngdc.cn.gov.cn.pngdc.cn http://www.morning.rknhd.cn.gov.cn.rknhd.cn http://www.morning.lveyue.com.gov.cn.lveyue.com http://www.morning.llxns.cn.gov.cn.llxns.cn http://www.morning.lfbsd.cn.gov.cn.lfbsd.cn http://www.morning.lwgrf.cn.gov.cn.lwgrf.cn http://www.morning.gnjtg.cn.gov.cn.gnjtg.cn http://www.morning.ghrlx.cn.gov.cn.ghrlx.cn http://www.morning.ujianji.com.gov.cn.ujianji.com http://www.morning.lthtp.cn.gov.cn.lthtp.cn http://www.morning.dyght.cn.gov.cn.dyght.cn http://www.morning.jydky.cn.gov.cn.jydky.cn