LeetCode112:Path Sum

正常写法

bool HasPathSum(TreeNode root, int sum) {
			bool ret=false;
			if(root==null)return false;
			if(root.left==null&&root.right==null) return root.val==sum;
			if(root.left!=null)
			{

				ret=ret|| HasPathSum(root.left,sum-root.val);
			}
			if(root.right!=null)
			{

				ret=ret|| HasPathSum(root.right,sum-root.val);
			}
			return ret;
		}

  破坏性写法

bool HasPathSum(TreeNode root, int sum) {
			bool ret=false;
			if(root==null)return false;
			if(root.left==null&&root.right==null) return root.val==sum;
			if(root.left!=null)
			{
				root.left.val+=root.val;
				ret=ret|| HasPathSum(root.left,sum);
			}
			if(root.right!=null)
			{
				root.right.val+=root.val;
				ret=ret|| HasPathSum(root.right,sum);
			}
			return ret;
		}

  在leetcode中第二种方法速度快,但是破坏了原树的值

时间: 2024-10-08 10:49:12

LeetCode112:Path Sum的相关文章

Project Euler 83:Path sum: four ways 路径和:4个方向

Path sum: four ways NOTE: This problem is a significantly more challenging version of Problem 81. In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by moving left, right, up, and down, is indicated in bold red an

Project Euler 80:Path sum: two ways 路径和:两个方向

Path sum: two ways In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by only moving to the right and down, is indicated in bold red and is equal to 2427.           131 673 234 103 18 201 96 342 965 150 630 803 74

leetcode笔记:Path Sum II

一.题目描述 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 二.题目分析 这道题与Path Su

LeetCode:Path Sum - 树的根节点到叶节点的数字之和

1.题目名称 Path Sum(树的根节点到叶节点的数字之和) 2.题目地址 https://leetcode.com/problems/path-sum/ 3.题目内容 英文: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. 中文:给定一颗二叉树,如

LeetCode之“树”:Path Sum && Path Sum II

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

leetcode笔记: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 tr

leetcode:Path Sum II

又一道DFS题,题意如下: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 非常容易,跟之前的打印

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

LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or righ