Leetcode#112Path 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 there exist a root-to-leaf path 5->4->11->2 which sum is 22.

分析,采用的策略,首先计算树的高度,然后申请最小为树高的内存空间构造栈结构。

采用先左子树优先遍历的方法,将树种的点放到栈中,直到遇到一个点没有左右子树,则判定为叶子结点,然后判断是否满足条件,满足返回结果true;不满足,弹出叶子结点,沿着路经回溯到上个节点,在将上个节点的右节点放入栈中分析。此处,我们使用的小trick是,对于已将左/右子树放入栈中的节点的相应的left/right位置,我们直接赋值为spec(遍历树找到设置为比最小值还小的值),那么回溯时,再遇到这个节点,能观测到的只有未放入栈中的子树。

/**

* Definition for binary tree

* public class TreeNode {

*     int val;

*     TreeNode left;

*     TreeNode right;

*     TreeNode(int x) { val = x; }

* }

*/

public class Solution {

public int height(TreeNode r)

{

if(r==null)

return 0;

else

{

int lh=height(r.left);

int rh=height(r.right);

if(lh>rh)

return lh+1;

else

return rh+1;

}

}

TreeNode[] stack;

int top;

int man;

public void push(TreeNode x)

{

/*

if(x!=null)

{

stack[top].val=x.val;

stack[top].left=x.left;

stack[top].right=x.right;

}

else

stack[top]=null;

*/

stack[top]=x;

top++;

}

public void pop()

{

top--;

}

public TreeNode getTop()

{

return stack[top-1];

}

public boolean isNull()

{

if(top==0)

return true;

else

return false;

}

public boolean hasPathSum(TreeNode root, int sum) {

if(root==null)

return false;

int s=0;

int h=height(root);

stack=new TreeNode[h+1];

for(int i=0;i<h+1;i++)

stack[i]=new TreeNode(0);

top=0;

man=h+1;

push(root);

while(!isNull())

{

TreeNode t=getTop();

s=s+t.val;

if(t.left==null&&t.right==null)

if(s==sum)

return true;

else

{

while(t.left==null&&t.right==null)

{

s=s-t.val;

pop();

if(isNull())

break;

t=getTop();

}

if(isNull())

break;

if(t.left!=null)

{

TreeNode tl=t.left;

t.left=null;

pop();

push(t);

push(tl);

}

if(t.right!=null)

{

TreeNode tr=t.right;

t.right=null;

pop();

push(t);

push(tr);

}

}

else if(t.left==null)

{

TreeNode tr=t.right;

t.right=null;

pop();

push(t);

push(tr);

}

else

{

TreeNode tl=t.left;

t.left=null;

pop();

push(t);

push(tl);

}

}

return false;

}

}

时间: 2024-10-27 19:10:41

Leetcode#112Path Sum的相关文章

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

leetcode -- 3 sum

3-sum 题目描述: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. 题目要求: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ 

[leetcode]Combination Sum @ Python

原题地址:https://oj.leetcode.com/problems/combination-sum/ 题意: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited

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: Combination Sum [038]

[题目] Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target)

LeetCode: Combination Sum II [039]

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

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] ] 给定一个二叉树和一个值,找出所有根到叶的路径和等于

Leetcode:Combination Sum 子集和问题

Combination Sum: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includ

LeetCode: Path Sum II [113]

[题目] 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] ] [题意] 判断二叉树中是否存在一条从根到