Leetcode#129Sum Root to Leaf Numbers

Sum Root to Leaf Numbers

Total Accepted: 43960 Total Submissions: 144900My Submissions

Question Solution

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.

An example is the root-to-leaf path 1->2->3 which represents the number 123.

Find the total sum of all root-to-leaf numbers.

For example,

    1
   /   2   3

The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.

Return the sum = 12 + 13 = 25.

分析:计算每条路经表示的数字的和,采用栈数据结构,存储路经过程中每一节点,以便遍历所有结果。遍历时,当使用完一个节点左子树,可以在这个位置处置空,在回溯时,不考虑为空的点,这样不会重复访问节点

public class Solution {

public int sumNumbers(TreeNode root) {

if(root==null)

return 0;

TreeNode x=root;

Stack<TreeNode> stack=new Stack<TreeNode>();

int sum=0;

int path=0;

stack.push(x);

path=path*10+x.val;

while(!stack.isEmpty()){

TreeNode y=stack.peek();

//stack.pop();

if(y.left!=null)

{

TreeNode z=y.left;

y.left=null;

stack.pop();

stack.push(y);

stack.push(z);

path=path*10+z.val;

}

else if(y.right!=null)

{

TreeNode z=y.right;

y.right=null;

stack.pop();

stack.push(y);

stack.push(z);

path=path*10+z.val;

}

else

{

sum=sum+path;

while(!stack.isEmpty())

{

TreeNode z=stack.peek();

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

{

path=path/10;

stack.pop();

}

else

break;

}

}

}

return sum;

}

}

时间: 2024-10-13 12:05:39

Leetcode#129Sum Root to Leaf Numbers的相关文章

[leetcode]_Sum Root to Leaf Numbers

题目:计算一棵二叉树所有路径组成的数的总和. 思考:也是DFS的基础应用.虽然还是套着别人的DFS框架写的,但是学习通常会经历先模拟,再创新的过程. 代码: 1 private int sum = 0; 2 public int sumNumbers(TreeNode root) { 3 dfs(root , 0); 4 return sum; 5 } 6 public void dfs(TreeNode node , int tempSum){ 7 if(node == null) retur

【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】

[129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represent

【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(所有根到叶子结点组组成的数字相加)】

[129-Sum Root to Leaf Numbers(所有根到叶子结点组组成的数字相加)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents

[leetcode]Sum Root to Leaf Numbers @ Python

原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Fin

LeetCode: Sum Root to Leaf Numbers [129]

[题目] Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 /

LeetCode——Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

[LeetCode] Sum Root to Leaf Numbers(bfs)

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

LeetCode :: Sum Root to Leaf Numbers [tree、dfs]

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

LeetCode: Sum Root to Leaf Numbers 解题报告

Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf number