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 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.

这道题,我是在先序遍历的基础上改的,添加了一个类似堆栈的list,如果是非叶子节点将其“入栈”,如果是叶子节点node“弹栈”直到node的父亲节点出现,扫描“栈底”到“栈顶”所有元素组成的数字,计算sum。直到前序遍历结束

 1 import java.util.ArrayList;
 2 import java.util.List;
 3
 4
 5
 6
 7    class TreeNode {
 8       int val;
 9       TreeNode left;
10       TreeNode right;
11       TreeNode(int x) { val = x; }
12   }
13
14 public class Solution {
15
16     List<TreeNode> list = new ArrayList<TreeNode>();
17
18     int sum = 0;
19
20     public int sumNumbers(TreeNode root) {
21         if(null == root)                                //空树返回0
22             return 0;
23         else if(null == root.left && null == root.right){//只有根节点返回根节点的值
24             return root.val;
25         }else{
26             preOrder(root);                                //至少有一个子树
27 //            list.add(root);                            //根节点入栈
28         }
29
30         return sum;
31     }
32
33     /**
34      * 前序遍历树
35      * @param root
36      */
37     public void preOrder(TreeNode root){
38         if(root == null)
39             return;
40         if(null == root.left && root.right == null){//叶子节点
41             while(list.size() != 0 && root != list.get(list.size() - 1).left && root != list.get(list.size() - 1).right){
42                 list.remove(list.size() - 1);
43             }
44             int temp = 0;
45             for(int i = 0; i < list.size(); i++){    //计算一个数
46                 temp = temp * 10 + list.get(i).val;
47             }
48             temp = temp * 10 + root.val;            //叶子节点加上去
49             //System.out.println("temp = " + temp);
50             sum += temp;                            //计算和
51             if(root == list.get(list.size() - 1).right){    //如果叶子节点是右子树,出栈
52                 list.remove(list.size() - 1);
53             }
54         }
55         else{    //非叶子节点
56             while(list.size() != 0 && root != list.get(list.size() - 1).left && root != list.get(list.size() - 1).right){
57                 list.remove(list.size() - 1);
58             }                        //root应该与栈顶元素相同
59             list.add(root);
60             preOrder(root.left);
61             preOrder(root.right);
62         }
63     }
64 }

题目提示的是用DFS,一会儿百度一下~

看了一下discuss里面的,这个写的挺漂亮的

public class Solution {
    public int sumNumbers(TreeNode root) {
        return preOrder(root, 0);
    }
    public int preOrder(TreeNode root, int val){
        if(root == null)
            return 0;
        val = val * 10 + root.val;
        if(null == root.left && null == root.right)
            return val;
        else
            return preOrder(root.left, val) + preOrder(root.right, val);
    }
}
时间: 2024-10-12 22:10:37

Sum Root to Leaf Numbers的相关文章

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刷题笔记】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 --day12 Surrounded Regions &amp; Sum Root to Leaf Numbers &amp; Longest Consecutive Sequence

1.  Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your fu

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

23. 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

【LeetCode OJ】Sum Root to Leaf Numbers

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # Definition for a  binary tree node # class TreeNode: #     def __init__(self, x): #         self.val = x #         self.left = No

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 the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2

[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 /